Why the inputs are not working right with ng-repeat
有人能给我解释一下为什么在这个简单的例子中我不能得到当前选中的单选按钮吗?我尝试使用ng repeat指令动态生成单选按钮,并使用ng模型选择当前的单选按钮。这样地:
模板:
1 2 3 | <input type="radio" name="movies" ng-value="kind" ng-model="kindSelected"> {{kind.name}} Selected Movie :{{kindSelected}} |
控制器:
1 2 3 4 5 6 7 8 9 10 | mymodule.controller('MainCtrl', [ '$scope',function ($scope) { $scope.movieKinds = [ {'name' : 'Action', 'movies' : ['Action#1', 'Action#2', 'Action#3']}, {'name' : 'Comedy', 'movies' : ['Comedy#1', 'Comedy#2', 'Comedy#3']}, {'name' : 'Drama', 'movies' : ['Drama#1', 'Drama#2']}, {'name' : 'Horror', 'movies' : ['Horror#1']} ]; }]); |
因为在每次迭代中,当在放置了
So what happens when
ng-repeat creates a new prototypically inherited child scope?In the child scope it carries all properties in which
primitive
property initial value taken while creating child scope & object
values are taken with their references so update in parent scope value
will update in child scope value & vice versa.
在本例中,您在
为了解决这个问题,您可以在定义
标记
1 2 3 | <input type="radio" name="movies" ng-value="kind" ng-model="kindSelected"> {{kind.name}} Selected Movie :{{model.kindSelected}} |
代码
1 | $scope.model = {}; |
解决这个问题的另一种方法是使用