2

I have a checkbox that should check all checkboxes. The checkbox works as it should by checking all the checkbox's, however angular doesnt think they have been checked? The only way angular knows if they are checked is if i manually check each one. (The brackets and for loop are blade php from laravel)

<label class="checkbox-inline">
  <input type="checkbox" ng-model="everyoneCheck"/> Everyone
</label>
@foreach($company->users as $tagIndex => $user)
    <label class="checkbox-inline">
      <input type="checkbox" ng-checked="everyoneCheck" ng-model="newDiscussion.notify_partners[{{$tagIndex}}]" ng-true-value="{{$user->id}}" /> {{ $user->first_name }} {{ $user->last_name }}
    </label>                  
@endforeach

upon click of the submit button i proceed to $http.post to my server, i just pass in an object to the post function, this is the object.

var discussionData = {
    'title': $scope.newDiscussion.title,
    'discussion': $scope.newDiscussion.summary,
    'company_id': company_id,
    'notify_partners': $scope.newDiscussion.notify_partners
};

for some reason when i use the check all approach, nothing gets put into notify_partners, however when i manually click each checkbox, they will get entered and submitted properly.

Any help? I feel like its some sort of binding issue, where i just need to tell angular, hey its updated!

3
  • Using ng-checked is only going to update the view, not the model Commented Feb 12, 2015 at 2:41
  • that makes sense why its not working then, any idea of a better approach? Commented Feb 12, 2015 at 2:43
  • Yeah, creating a function and looping over model values (see my answer below) Commented Feb 12, 2015 at 2:46

2 Answers 2

1

Here's a way to do it:

<p><input type="checkbox" ng-model="globalCheck" ng-click="toggleCheckAll()" /> Check All</p>

<ul>
    <li ng-repeat="i in init">
        <input type="checkbox" ng-model="checkbox[$index]" /> Checkbox {{ $index + 1 }} {{ checkbox[$index] }} 
    </li>
</ul>

Then in your controller:

function myControl($scope) {

    $scope.globalCheck = false;
    $scope.checkbox = {};
    $scope.init = [0,1,2,3,4,5,6,7,8,9];

    $scope.toggleCheckAll = function() {
        var k, val = !$scope.globalCheck;
        console.log(val);
        for(k in $scope.init) {
            $scope.checkbox[k] = val;
        }
    }

}

See JSfiddle for working example

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

4 Comments

Just fixed a typo in the above ($scope.newDiscussion.notify_partners[k])
This code doesnt seem to check them all, not sure why. $scope.checkAll = function () { var val = $scope.everyoneCheck; for (var i = 0; i < $scope.newDiscussion.notify_partners.length; i++) { $scope.newDiscussion.notify_partners[i] = val; } }
the for loop isn't running because $scope.newDiscussion.notify_partners doesnt have anything in it apparently. The first time the property is populated is in the view, which is the code you saw up top. I originally initiate the property in my init function with $scope.newDiscussion.notify_partners = [];
If i check the check boxes then click the check all it removes them the checks, it just wont add them?
0

ng-checked does not update the value bound in ng-model. It only affects the presence of the checked attribute on the element itself.

Your best bet is to use ng-change to execute some function and update all your models accordingly.

<input type="checkbox" 
   ng-model="everyoneCheck"
   ng-change="toggleCheckAll()"/>

And in your controller, you can have toggleCheckAll() loop over your models and set them based on the value of everyoneCheck

Comments

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.