1

I have a select all checkbox and list check-box like this.
Select All Checkbox

A checkbox list receive data from

$scope.contacts = [
        {"name": "Bambizo", "check": false},
        {"name": "Jimmy", "check": false},
        {"name": "Tommy", "check": false},
        {"name": "Nicky", "check": false}
];

I want when i check a Select all checkbox, it make all checkbox in below list are checked or unchecked. And here my code:

Select All Checkbox:

<input type="checkbox" ng-model="checkAllContact" ng-change="checkAllContact(checkAllContact)">

checkAllContact function:

$scope.checkAllContact = function(){
        var allChecked = false;
        for(i = 0; i< $scope.contacts.length; i++){
            if ($scope.contacts[i].check == true){
                allChecked = true;
            }else {
                allChecked = false;
            }
        }

        if (allChecked = true){
            for(i = 0; i< $scope.contacts.length; i++){
                $scope.contacts[i].check = false;
            }
        }else{
            for(i = 0; i< $scope.contacts.length; i++){
                $scope.contacts[i].check = true;
            }    
        }
    }

But when i run and click Select All checkbox. It make an error:

Error when after click checkbox

How to solve it or have any other way to do it? Thanks

2
  • Have you tried ng-change="checkAllContact()"? Commented May 8, 2015 at 9:32
  • It still make a error: "TypeError: Cannot read property '0' of null" Commented May 8, 2015 at 9:35

2 Answers 2

2

ng-model="checkAllContact" & method checkAllContact has same name.

checkAllContact scope variable is overriding by checkAllContact.

You need to change your function name will fix the issue.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks.It not make an error. But my function still not run :(. Can i run a function when check a checkbox?
@PhamMinhTan did you change your function name?
Yes, I did it. method and variables is different
@PhamMinhTan could you update your question with what you have tried till
@PhamMinhTan when you are going to do that.?
|
1

Your function name and variable name(ng-model) both are same, change one of them.

Also you can do it in a much simpler way, for an eg.

HTML:

 <input type="checkbox" ng-model="checkAllContact1" ng-change="checkAllContact()">
 <div ng-repeat="item in contacts">
     <input type="checkbox" ng-model="item.check"/>
 </div>

JS:

$scope.checkAllContact = function(){
    if ($scope.checkAllContact1) {
        $scope.checkAllContact1 = true;
    } else {
        $scope.checkAllContact1 = false;
    }
    angular.forEach($scope.contacts, function (item) {
        item.check = $scope.checkAllContact1;
    });
}

See the example Fiddle

3 Comments

I try to do like your answer. But my $scope.checkAllContact1 is always false.
@PhamMinhTan. Its working in my fiddle isn't it? If its still not working, can you provide a fiddle?
Yes. your fiddle is running well. I will update my code in fiddle. Thanks.

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.