I am trying to bind an object to a radio button group. Please refer the two snippets of code in this post. The first uses ng-repeat to loop but it fails. I manually repeat the element in the second example but it works. Why does ng-repeat cause this issue?
Example 1
angular
.module('listParser', [])
.factory('Item', function() {
return function(id, name) {
this.id = id;
this.name = name;
}
})
.controller('default', ['$scope', 'Item', function($scope, Item) {
var collection = [];
collection.push(new Item(0, 'Alpha'));
collection.push(new Item(1, 'Beta'));
$scope.collection = collection;
$scope.lucky = collection[0];
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="listParser" ng-controller="default">
<div class="radio-group">
<ul><li ng-repeat="item in collection"><input type="radio" name="group" ng-value="item" ng-model="lucky"/></li></ul>
</div>
<div class="description">
{{lucky.name}}
</div>
</div>
Example 2
angular
.module('listParser', [])
.factory('Item', function() {
return function(id, name) {
this.id = id;
this.name = name;
}
})
.controller('default', ['$scope', 'Item', function($scope, Item) {
var collection = [];
collection.push(new Item(0, 'Alpha'));
collection.push(new Item(1, 'Beta'));
$scope.collection = collection;
$scope.lucky = collection[0];
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="listParser" ng-controller="default">
<div class="radio-group">
<ul>
<li><input type="radio" name="group" ng-value="collection[0]" ng-model="lucky"/></li>
<li><input type="radio" name="group" ng-value="collection[1]" ng-model="lucky"/></li>
</ul>
</div>
<div class="description">
{{lucky.name}}
</div>
</div>