我有这样的代码
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复选框,然后单击所有单独的项目,它将不会按我的预期工作(checkall复选框将不会根据选择进行选中或取消选中)
我尝试了一些堆栈溢出的答案,但都是一样的。有人能告诉我怎么做吗。请
这里的主要问题是,您将ng模型分配给"checkall"复选框,然后单击ng调用一个函数,在该函数中您再次定义$scope.checkall的值。
您不需要这样做,因为ng模型已经为您设置了该变量的值。
看看这个小提琴,使用你提供的相同代码,但有修复:https://jsfiddle.net/h46rLzhs/5/
angular.module('MyApp',[])
.controller('MyController', function($rootScope, $scope){
$rootScope.selected=[];
$scope.allCheckBox=[];
$scope.payout= [
{'_id':1, value:'Option1'},
{'_id':2, value:'Option2'}
];
$scope.toggleAll = function(payout) {
// Line below is not needed
//$scope.checkall = !$scope.checkall;
for(var i in payout){
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');
}
}
})
<div ng-app="MyApp">
<div ng-controller="MyController">
<div ng-click="toggleAll(payout)">
select all
<input type="checkbox" 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>
<input type="checkbox" ng-model="allCheckBox[$index]" ng-click="selectItem(item._id,selected,payout)">
</td>
</tr>
</tbody>
</table>
</div>
</div>
我修改了您的代码使其工作:
$scope.checkall=false;
$scope.allCheckBox=[];
$rootScope.selected = [];
$scope.payout = [{
'_id': 1,
value: 'Option1'
}, {
'_id': 2,
value: 'Option2'
}];
$scope.toggleAll = function() {
$scope.checkall = !$scope.checkall;
$rootScope.selected = [];
$scope.allCheckBox=[];
for (var i = 0; i < $scope.payout.length; i++) {
$scope.allCheckBox.push($scope.checkall);
if ($scope.checkall)
$rootScope.selected.push($scope.payout[i]);
}
}
$scope.selectItem = function(id) {
console.log('id', id);
var idx = $rootScope.selected.indexOf(id);
if (idx > -1) {
$rootScope.selected.splice(idx, 1);
} else {
$rootScope.selected.push(id);
}
if ($scope.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');
}
}
html:
<div class="check_toggle" ng-click="toggleAll()">
select all
<input type="checkbox" aria-label="Art" ng-model="checkall" ng-click="toggleAll()"/>
</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)">
</div>
</td>
</tr>
</tbody>
</table>
Plunk:https://plnkr.co/edit/SmabE9?p=preview
我没有读过你的问题,但要切换所有复选框,这段代码就可以了。这是你必须操作它的例子。你可以看看这里。https://plnkr.co/edit/qD7CABUF9GLoFCb8vDuO?p=preview
var app = angular.module("test", []);
app.controller('testctrl',function($scope){
$scope.options=[
{option:"option1",
selected:false ,
},
{option:"option1",
selected:false ,
},
{option:"option1",
selected:false ,
},
{option:"option1",
selected:false ,
},
{option:"option1",
selected:false ,
},
]
$scope.test=function(event){
if(event.currentTarget.checked==true)
{
var togglestatus=$scope.isAllSelected;
angular.forEach($scope.options,function(object){
object.selected=togglestatus
})
<body ng-app="test" ng-controller="testctrl">
<label>
<input type="checkbox" ng-model="isAllSelected" ng-click="test($event)">SelectAll</label>
<div ng-repeat="option in options">
<input type="checkbox" ng-model="option.selected">checkbox</div>
</body>
}else{
angular.forEach($scope.options,function(object){
object.selected=false
})
}
}
})