选择/取消选中所有复选框元素AngularJS

时间:2016-07-18 10:29:55

标签: javascript html angularjs checkbox

我在AngularJS中有一个带有复选框的项目列表,我有一个可以选择所有项目的复选框。该功能仅用于选择所有项目,我想要的是修改它以取消选择所有项目。

这是我的功能:

$scope.items.allItemsSelected = false;
$scope.selectAll = function() {
    for (var i = 0; i < $scope.itemsList.length; i++) {
        $scope.temp.push($scope.itemsList[i].name);
        console.log($scope.itemsList[i].name);
        $scope.itemsList[i].isChecked = $scope.items.allItemsSelected;
    }
};

这是我的HTML:

<md-checkbox ng-model="items.allItemsSelected"
             ng-change="selectAll()">
    Select all
</md-checkbox>

有没有人知道如何修改它以取消选择元素?

提前致谢!

1 个答案:

答案 0 :(得分:0)

使用ng-checked属性。

<md-checkbox ng-model="items.allItemsSelected" ng-change="selectAll()">
    Select all
</md-checkbox>

checkbox1 -

<md-checkbox ng-checked="isSelectAll" ng-model="items.allItemsSelected" ng-change="selectAll()">
    Select all
</md-checkbox>

checkbox2 -

<md-checkbox ng-checked="isSelectAll" ng-model="items.allItemsSelected" ng-change="selectAll()">
    Select all
</md-checkbox>

JS:

$scope.isSelectAll = false;
$scope.selectAll = function() {
    if (!$scope.isSelectAll) {
        $scope.isSelectAll = true;
    } else {
        $scope.isSelectAll = false;
    }
}
相关问题