1

How can I check if more than one checkbox is checked in my controller?
fiddle

My markup:

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

My Code:

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

1 Answer 1

2

You have bound the checkbox state for each item to item.Selected, so if checkbox is selected for an item, item.Selected should be true.

You just have to count items where the Selected attribute is set to 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;
};
Sign up to request clarification or add additional context in comments.

1 Comment

It's not working because checkAll is never called... :) Look here jsfiddle.net/p7kmxa8x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.