如何检查angularjs中是否选中了多个复选框

时间:2015-03-02 08:24:14

标签: angularjs

如何检查控制器中是否选中了多个复选框?
fiddle

我的标记:

<li ng-repeat="item in Items">
    <label>{{item.Name}}
        <input type="checkbox" ng-model="item.Selected" />
    </label>
</li>

我的代码:

$scope.checkAll = function () {
    //i want to check if checkbox checked more than 1
    angular.forEach($scope.Items, function (item) {
        item.Selected = $scope.selectedAll;
    });
};

1 个答案:

答案 0 :(得分:2)

您已将每个项目的复选框状态绑定到item.Selected,因此如果为某个项目选中了复选框,则item.Selected应为true。

您只需计算Selected属性设置为true的项目。

<li ng-repeat="item in Items">
    <label>{{item.Name}}
        <input type="checkbox" ng-model="item.Selected" />
    </label>
</li>

// Returns the count of selected items
$scope.checkAll = function () {
    var count = 0;

    angular.forEach($scope.Items, function (item) {
        if (item.Selected) count++;
    });

    return count;
};