我有这样的代码 的 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复选框)。
我尝试了一些堆栈溢出答案,但它是一样的。谁能告诉我如何。请
答案 0 :(得分:2)
我修改了您的代码以使其正常工作:
$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>
答案 1 :(得分:2)
这里的主要问题是你将ng-model分配给“checkall”复选框和ng-click调用一个函数,在该函数中你再次定义$ scope.checkall的值。
您不需要这样做,因为ng-model已经为您设置了该变量的值。
使用您提供的相同代码查看此小提琴,但使用修复程序: 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>
答案 2 :(得分:1)
我没有读过你的问题,但要切换所有复选框,这段代码将起作用。这是你必须操纵它的例子。你可以看看这里。 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
})
}
}
})