Angularjs 棱角;大师;选中所有复选框,设置所有其他复选框的选中状态
我想这样做:Angularjs 棱角;大师;选中所有复选框,设置所有其他复选框的选中状态,angularjs,Angularjs,我想这样做: [ ] all toppings ----------------- [ ] mushrooms [ ] peppers [ ] extra cheese [ ] artichokes 当用户选中或取消选中[]所有浇头时,单个浇头要么全部选中,要么全部取消选中 各个复选框工作正常。但是,当控制器在setAll()方法中迭代toppings列表,设置每个列表的选定状态时,UI不会显示更改 $sco
[ ] all toppings
-----------------
[ ] mushrooms
[ ] peppers
[ ] extra cheese
[ ] artichokes
当用户选中或取消选中[]所有浇头时,单个浇头要么全部选中,要么全部取消选中
各个复选框工作正常。但是,当控制器在setAll()
方法中迭代toppings列表,设置每个列表的选定状态时,UI不会显示更改
$scope.items = [{id: 100, name: "mushrooms", price: 1.00}, ....]
<div>
<p><input type="checkbox" id="allToppings"
data-ng-click="setAll($event)"> all
</p>
<hr/>
<p data-ng-repeat="item in items">
<input type="checkbox" data-ng-model="item.selected"
data-ng-click="item.selected=!item.selected"> {{item.name}}
</p>
</div>
$scope.setAll = function (e) {
for (item in items) {
item.selected = (e.target.checked);
}
}
$scope.items=[{id:100,名称:“蘑菇”,价格:1.00},…]
全部
{{item.name}
$scope.setAll=函数(e){
用于(项目中的项目){
item.selected=(e.target.checked);
}
}
调用
setAll()
方法,并正确设置每个项的选定属性。让UI显示更改的正确方式是什么?是否需要对toppings数组进行监视?在对(项目中的项目)
这样的数组进行迭代时,项目将是每个项目的索引。试试这个:
for (var i = 0; i < items.length; i++) {
items[i].selected = (e.target.checked);
}
for(变量i=0;i
您可能希望使用框架本身的功能
谢谢你指出错误。我可以在8分钟内接受你的回答:-)不应该在数组上使用for in
$scope.setAll = function (e) {
angular.forEach(items, function(item, key) {
item.selected = e.target.checked;
});
}