Heim > Fragen und Antworten > Hauptteil
从后端获取数组如下:
$scope.arr = [
{
selected:true;
}
{
selected:true;
}
{
selected:true;
}
]
$scope.select = function(){
// 如何在不调用后端接口的情况下,修改ng-repeat中ng-checked的状态实现全选和反选?
}
<ul>
<li ng-repeat="i in arr">
<input type="checkbox" ng-checked="i.selected">
</li>
</ul>
<input type="checkbox" ng-click="select()">
给我你的怀抱2017-05-15 17:05:50
$scope.selectedAll = true;
$scope.arr = [
{
selected:true;
}
{
selected:true;
}
{
selected:true;
}
]
$scope.select = function(){
$scope.selectedAll = !$scope.selectedAll;
$scope.arr.forEach(function(item){
item.selected = $scope.selectedAll;
});
};
为情所困2017-05-15 17:05:50
$scope.arr = [
{ selected:true }
{ selected:true }
{ selected:true }
];
$scope.select = function(status){
$scope.arr.map(function(item){
item.selected = status === 'all'? true: !item.selected;
});
}
<ul>
<li ng-repeat="i in arr">
<input type="checkbox" ng-checked="i.selected">
</li>
</ul>
<button ng-click="select('all')">全选</button>
<button ng-click="select('inverse')">反选</button>
这里边的反选说的是在不全选的情况下,选中变为未选中,未选中变选中,不是清空!!!
清空的话要复杂一点。
某草草2017-05-15 17:05:50
http://stackoverflow.com/questions/12648466/how-can-i-get-angular-js-checkboxes-with-select-unselect-all-functionality-and-i