在AngularJS中选择所有复选框 [英] Select All Checkbox in AngularJS
问题描述
我有一个全选复选框,并列出了这样的复选框。
I have a select all checkbox and list check-box like this.
一个复选框列表从
$scope.contacts = [
{"name": "Bambizo", "check": false},
{"name": "Jimmy", "check": false},
{"name": "Tommy", "check": false},
{"name": "Nicky", "check": false}
];
我想要当我选中全选复选框时,使下面列表中的所有复选框处于选中状态或未选中状态。这是我的代码:
I want when i check a Select all checkbox, it make all checkbox in below list are checked or unchecked. And here my code:
选择所有复选框:
<input type="checkbox" ng-model="checkAllContact" ng-change="checkAllContact(checkAllContact)">
checkAllContact函数:
$scope.checkAllContact = function(){
var allChecked = false;
for(i = 0; i< $scope.contacts.length; i++){
if ($scope.contacts[i].check == true){
allChecked = true;
}else {
allChecked = false;
}
}
if (allChecked = true){
for(i = 0; i< $scope.contacts.length; i++){
$scope.contacts[i].check = false;
}
}else{
for(i = 0; i< $scope.contacts.length; i++){
$scope.contacts[i].check = true;
}
}
}
但是当我运行并单击选择所有复选框。它会出错:
But when i run and click Select All checkbox. It make an error:
如何解决或有其他解决方法?谢谢
How to solve it or have any other way to do it? Thanks
推荐答案
ng-model = checkAllContact
&方法 checkAllContact
具有相同的名称。
ng-model="checkAllContact"
& method checkAllContact
has same name.
checkAllContact
作用域变量被 checkAllContact
覆盖。
checkAllContact
scope variable is overriding by checkAllContact
.
您需要更改函数名称才能解决此问题。
You need to change your function name will fix the issue.
这篇关于在AngularJS中选择所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!