【发布时间】:2016-12-07 20:12:13
【问题描述】:
我有这样的代码
HTML
<div class="check_toggle" ng-click="toggleAll(payout)">
select all
<input type="checkbox" aria-label="Art" ng-model="checkall"/>
</div>
<table>
<thead>
<tr>
<th>Week</th>
<th>Release Payment</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in payout">
<td>{{item.value}}</td>
<td>
<div class="checkbox pay_check">
<input aria-label="Art" type="checkbox" ng-model="allCheckBox[$index]" ng-click="selectItem(item._id,selected,payout)">
</div>
</td>
</tr>
</tbody>
控制器
$scope.payout= [
{'_id':1, value:'Option1'},
{'_id':2, value:'Option2'}
];
$scope.toggleAll = function(payout) {
$scope.checkall = !$scope.checkall;
for(var i=0;i<payout.length;i++){
if($scope.checkall===false){
$rootScope.selected=[];
$scope.allCheckBox[i] = $scope.checkall ;
}else{
$rootScope.selected[i]=payout[i]._id;
$scope.allCheckBox[i] =$scope.checkall ;
}
}
}
$scope.selectItem = function(id, list,payout) {
console.log('id',id);
var idx = list.indexOf(id);
if (idx > -1) {
list.splice(idx, 1);
} else {
list.push(id);
}
if(payout.length==$rootScope.selected.length){
$scope.checkall=true;
console.log($scope.checkall);
// $scope.checkall= $scope.checkall;
console.log('All checkboxes selected');
}
else{
$scope.checkall=false;
console.log('Not All checkboxes selected');
}
}
我有使用 ng-repeat 的单独复选框,并选择所有复选框。首先,当我选择所有单独的选择框时,复选框将按我的预期自动检查,并且还检查所有复选框,并按我的预期选择所有单独的复选框,但问题是,如果我先单击 checkall 复选框,然后单击所有单个项目,它将无法按预期工作(将不会根据选择选中或取消选中 checkall 复选框)。
我尝试了一些堆栈溢出的答案,但它是一样的。谁能告诉我怎么做。请
【问题讨论】:
标签: javascript html angularjs checkbox angular-material