什麼也不多說了,直接上代碼
<ul ng-repeat="d in data">
<li><input type="checkbox" ng-model="select_all" ng-click="selectAll($event)"></li>
<li><input type="checkbox" ng-checked="isSelected(d.id) || select_all" ng-click="updateSelection($event, d.id)"></li>
</ul>
js代碼
$scope.data = {
id: 1,
id: 2,
id: 3,
id: 4
}
$scope.selected = [];
var updateSelected = function(action, id) {
if(action == 'add' & $scope.selected.indexOf(id) == -1) $scope.selected.push(id);
if(action == 'remove' && $scope.selected.indexOf(id) != -1) vm.selected.splice($scope.selected.indexOf(id), 1);
};
vm.updateSelection = function($event, id) {
var checkbox = $event.target;
var action = (checkbox.checked ? 'add' : 'remove');
updateSelected(action, id);
};
vm.isSelected = function(id) {
return $scope.selected.indexOf(id) >= 0;
}; /*checkbox選中*/
$scope.selectAll = function() {
$scope.selected = []
if($scope.select_all) {
angular.forEach(vm.data.list, function (i) {
$scope.selected.push(i.id);
})
}
};