admin管理员组文章数量:1356889
The problem is in binding the state of the checkbox (checked/unchecked) to the object values.
HTML:
<div ng:controller="Ctrl">
<div ng:repeat="(letter, number) in obj">
{{letter}} and {{number}}
<input type="checkbox" ng:model="obj[letter]">
</div>
Controller:
function Ctrl() {
this.obj = {a:true,b:true};
};
When the first checkbox is clicked it affects the state of the second checkbox, but the model is correct, so obj bees {a:false, b:true}.
Example can be found at /
How to fix this?
The problem is in binding the state of the checkbox (checked/unchecked) to the object values.
HTML:
<div ng:controller="Ctrl">
<div ng:repeat="(letter, number) in obj">
{{letter}} and {{number}}
<input type="checkbox" ng:model="obj[letter]">
</div>
Controller:
function Ctrl() {
this.obj = {a:true,b:true};
};
When the first checkbox is clicked it affects the state of the second checkbox, but the model is correct, so obj bees {a:false, b:true}.
Example can be found at http://jsfiddle/alexpetrov/tRxzr/
How to fix this?
Share Improve this question asked May 4, 2012 at 10:46 petrov.alexpetrov.alex 1,0892 gold badges12 silver badges21 bronze badges2 Answers
Reset to default 6Bind ng-repeat to objects rather than primitive types.
function Ctrl() {
this.obj = [{id: 'a', checked: true}, {id: 'b', checked: true}];
}
http://jsfiddle/tRxzr/1/
Binding to primitive types confuses ng-repeat, it's a bug: https://github./angular/angular.js/issues/933
When the JSON is not entirely in your control, you get a primitive array instead of an object. You want to do an ng-repeat on the same.
To bind ng-repeats to checkboxes to a primitive array and get the selected items. See the plunker code here.
http://plnkr.co/edit/i6IiGY42h8CiOMaqT9SZ?p=preview
本文标签: javascriptBinding checkboxes to object values in AngularJsStack Overflow
版权声明:本文标题:javascript - Binding checkboxes to object values in AngularJs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743972973a2570763.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论