AngularJS radio button groups with name attribute not initializing properly
up vote
0
down vote
favorite
The radio buttons group doesn't seem to initialize property inside a ng-repeat block if we specify the name attribute to the group. It seems working for the last set of radio groups, but not for the rest. Also, it works fine if I remove the name attribute. Per [AngularJS documentation][1], the name attribute is not needed if we use it with ngModel. But it doesn't say we must not use it. And I need to specify a name attribute.
Please refer to the example below:
var m = angular.module("MyApp", );
m.controller("MyController", ["$scope", function($scope) {
$scope.myArr = [{
name: "Obj 1",
status: "a"
}, {
name: "Obj 2",
status: "b"
}, {
name: "Obj 3",
status: "c"
}];
}]);body {
background-color: #1D1F20;
}
.row {
margin-bottom: 15px;
}
.demo {
background: green;
color: white;
padding: 10px;
border-radius: 3px;
min-width: 200px;
}<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp" ng-controller="MyController" class="demo">
<div ng-repeat="eachItem in myArr" class="row">
<span>{{eachItem.name}} => </span>
<label> a
<input name="status-$index" type="radio" ng-model="eachItem.status" value="a">
</label>
<label> b
<input name="status-$index" type="radio" ng-model="eachItem.status" value="b">
</label>
<label> c
<input name="status-$index" type="radio" ng-model="eachItem.status" value="c">
</label>
</div>
</div>angularjs angularjs-ng-repeat radio-group
add a comment |
up vote
0
down vote
favorite
The radio buttons group doesn't seem to initialize property inside a ng-repeat block if we specify the name attribute to the group. It seems working for the last set of radio groups, but not for the rest. Also, it works fine if I remove the name attribute. Per [AngularJS documentation][1], the name attribute is not needed if we use it with ngModel. But it doesn't say we must not use it. And I need to specify a name attribute.
Please refer to the example below:
var m = angular.module("MyApp", );
m.controller("MyController", ["$scope", function($scope) {
$scope.myArr = [{
name: "Obj 1",
status: "a"
}, {
name: "Obj 2",
status: "b"
}, {
name: "Obj 3",
status: "c"
}];
}]);body {
background-color: #1D1F20;
}
.row {
margin-bottom: 15px;
}
.demo {
background: green;
color: white;
padding: 10px;
border-radius: 3px;
min-width: 200px;
}<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp" ng-controller="MyController" class="demo">
<div ng-repeat="eachItem in myArr" class="row">
<span>{{eachItem.name}} => </span>
<label> a
<input name="status-$index" type="radio" ng-model="eachItem.status" value="a">
</label>
<label> b
<input name="status-$index" type="radio" ng-model="eachItem.status" value="b">
</label>
<label> c
<input name="status-$index" type="radio" ng-model="eachItem.status" value="c">
</label>
</div>
</div>angularjs angularjs-ng-repeat radio-group
1
Try <input name="status-{{ $index }}" type="radio" ng-model="eachItem.status" value="a">
– Mickers
Nov 19 at 17:31
Awesome. That worked. Thanks @Mickers.
– Manoj Shrestha
Nov 19 at 17:37
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
The radio buttons group doesn't seem to initialize property inside a ng-repeat block if we specify the name attribute to the group. It seems working for the last set of radio groups, but not for the rest. Also, it works fine if I remove the name attribute. Per [AngularJS documentation][1], the name attribute is not needed if we use it with ngModel. But it doesn't say we must not use it. And I need to specify a name attribute.
Please refer to the example below:
var m = angular.module("MyApp", );
m.controller("MyController", ["$scope", function($scope) {
$scope.myArr = [{
name: "Obj 1",
status: "a"
}, {
name: "Obj 2",
status: "b"
}, {
name: "Obj 3",
status: "c"
}];
}]);body {
background-color: #1D1F20;
}
.row {
margin-bottom: 15px;
}
.demo {
background: green;
color: white;
padding: 10px;
border-radius: 3px;
min-width: 200px;
}<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp" ng-controller="MyController" class="demo">
<div ng-repeat="eachItem in myArr" class="row">
<span>{{eachItem.name}} => </span>
<label> a
<input name="status-$index" type="radio" ng-model="eachItem.status" value="a">
</label>
<label> b
<input name="status-$index" type="radio" ng-model="eachItem.status" value="b">
</label>
<label> c
<input name="status-$index" type="radio" ng-model="eachItem.status" value="c">
</label>
</div>
</div>angularjs angularjs-ng-repeat radio-group
The radio buttons group doesn't seem to initialize property inside a ng-repeat block if we specify the name attribute to the group. It seems working for the last set of radio groups, but not for the rest. Also, it works fine if I remove the name attribute. Per [AngularJS documentation][1], the name attribute is not needed if we use it with ngModel. But it doesn't say we must not use it. And I need to specify a name attribute.
Please refer to the example below:
var m = angular.module("MyApp", );
m.controller("MyController", ["$scope", function($scope) {
$scope.myArr = [{
name: "Obj 1",
status: "a"
}, {
name: "Obj 2",
status: "b"
}, {
name: "Obj 3",
status: "c"
}];
}]);body {
background-color: #1D1F20;
}
.row {
margin-bottom: 15px;
}
.demo {
background: green;
color: white;
padding: 10px;
border-radius: 3px;
min-width: 200px;
}<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp" ng-controller="MyController" class="demo">
<div ng-repeat="eachItem in myArr" class="row">
<span>{{eachItem.name}} => </span>
<label> a
<input name="status-$index" type="radio" ng-model="eachItem.status" value="a">
</label>
<label> b
<input name="status-$index" type="radio" ng-model="eachItem.status" value="b">
</label>
<label> c
<input name="status-$index" type="radio" ng-model="eachItem.status" value="c">
</label>
</div>
</div>var m = angular.module("MyApp", );
m.controller("MyController", ["$scope", function($scope) {
$scope.myArr = [{
name: "Obj 1",
status: "a"
}, {
name: "Obj 2",
status: "b"
}, {
name: "Obj 3",
status: "c"
}];
}]);body {
background-color: #1D1F20;
}
.row {
margin-bottom: 15px;
}
.demo {
background: green;
color: white;
padding: 10px;
border-radius: 3px;
min-width: 200px;
}<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp" ng-controller="MyController" class="demo">
<div ng-repeat="eachItem in myArr" class="row">
<span>{{eachItem.name}} => </span>
<label> a
<input name="status-$index" type="radio" ng-model="eachItem.status" value="a">
</label>
<label> b
<input name="status-$index" type="radio" ng-model="eachItem.status" value="b">
</label>
<label> c
<input name="status-$index" type="radio" ng-model="eachItem.status" value="c">
</label>
</div>
</div>var m = angular.module("MyApp", );
m.controller("MyController", ["$scope", function($scope) {
$scope.myArr = [{
name: "Obj 1",
status: "a"
}, {
name: "Obj 2",
status: "b"
}, {
name: "Obj 3",
status: "c"
}];
}]);body {
background-color: #1D1F20;
}
.row {
margin-bottom: 15px;
}
.demo {
background: green;
color: white;
padding: 10px;
border-radius: 3px;
min-width: 200px;
}<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp" ng-controller="MyController" class="demo">
<div ng-repeat="eachItem in myArr" class="row">
<span>{{eachItem.name}} => </span>
<label> a
<input name="status-$index" type="radio" ng-model="eachItem.status" value="a">
</label>
<label> b
<input name="status-$index" type="radio" ng-model="eachItem.status" value="b">
</label>
<label> c
<input name="status-$index" type="radio" ng-model="eachItem.status" value="c">
</label>
</div>
</div>angularjs angularjs-ng-repeat radio-group
angularjs angularjs-ng-repeat radio-group
edited Nov 19 at 17:33
asked Nov 19 at 17:15
Manoj Shrestha
1,39411832
1,39411832
1
Try <input name="status-{{ $index }}" type="radio" ng-model="eachItem.status" value="a">
– Mickers
Nov 19 at 17:31
Awesome. That worked. Thanks @Mickers.
– Manoj Shrestha
Nov 19 at 17:37
add a comment |
1
Try <input name="status-{{ $index }}" type="radio" ng-model="eachItem.status" value="a">
– Mickers
Nov 19 at 17:31
Awesome. That worked. Thanks @Mickers.
– Manoj Shrestha
Nov 19 at 17:37
1
1
Try <input name="status-{{ $index }}" type="radio" ng-model="eachItem.status" value="a">
– Mickers
Nov 19 at 17:31
Try <input name="status-{{ $index }}" type="radio" ng-model="eachItem.status" value="a">
– Mickers
Nov 19 at 17:31
Awesome. That worked. Thanks @Mickers.
– Manoj Shrestha
Nov 19 at 17:37
Awesome. That worked. Thanks @Mickers.
– Manoj Shrestha
Nov 19 at 17:37
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Your code looks fine except the place where you are setting the name attribute to input.
$index in template will be evaluated only if you enclose it inside curly brackets.
EX: {{$index}} else you will get $index in the name. You should use:
<div ng-repeat="eachItem in myArr track by $index" class="row">
<input name="status-{{$index}}" type="radio" ng-model="eachItem.status" value="a">...
</div>
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Your code looks fine except the place where you are setting the name attribute to input.
$index in template will be evaluated only if you enclose it inside curly brackets.
EX: {{$index}} else you will get $index in the name. You should use:
<div ng-repeat="eachItem in myArr track by $index" class="row">
<input name="status-{{$index}}" type="radio" ng-model="eachItem.status" value="a">...
</div>
add a comment |
up vote
1
down vote
accepted
Your code looks fine except the place where you are setting the name attribute to input.
$index in template will be evaluated only if you enclose it inside curly brackets.
EX: {{$index}} else you will get $index in the name. You should use:
<div ng-repeat="eachItem in myArr track by $index" class="row">
<input name="status-{{$index}}" type="radio" ng-model="eachItem.status" value="a">...
</div>
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Your code looks fine except the place where you are setting the name attribute to input.
$index in template will be evaluated only if you enclose it inside curly brackets.
EX: {{$index}} else you will get $index in the name. You should use:
<div ng-repeat="eachItem in myArr track by $index" class="row">
<input name="status-{{$index}}" type="radio" ng-model="eachItem.status" value="a">...
</div>
Your code looks fine except the place where you are setting the name attribute to input.
$index in template will be evaluated only if you enclose it inside curly brackets.
EX: {{$index}} else you will get $index in the name. You should use:
<div ng-repeat="eachItem in myArr track by $index" class="row">
<input name="status-{{$index}}" type="radio" ng-model="eachItem.status" value="a">...
</div>
answered Nov 20 at 21:28
nkuma_12
53712
53712
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379646%2fangularjs-radio-button-groups-with-name-attribute-not-initializing-properly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Try <input name="status-{{ $index }}" type="radio" ng-model="eachItem.status" value="a">
– Mickers
Nov 19 at 17:31
Awesome. That worked. Thanks @Mickers.
– Manoj Shrestha
Nov 19 at 17:37