Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I have a group of material design checkboxes and I would like to bind their values to an array in my controller.

To accomplish this I used the first method described in this SO answer. While the values are properly being added and removed from the list, the boxes no longer display as "checked" when I click on them. My code is below and I also recreated the problem in this codepen.

HTML for my checkbox

<md-checkbox 
    ng-repeat="site in websites" 
    value="{{site}}" 
    ng-checked="selection.indexOf(site) > -1" 
    ng-click="toggleSelection(site)"> 
    {{site}}
</md-checkbox>

JavaScript from Controller

  $scope.websites = ['Facebook', 'Twitter', 'Amazon'];
  $scope.selection = ['Facebook'];
  $scope.toggleSelection = function toggleSelection(site) {
    var idx = $scope.selection.indexOf(site);

    // is currently selected
    if (idx > -1) {
      $scope.selection.splice(idx, 1);
    }

    // is newly selected
    else {
      $scope.selection.push(site);
    }
  };
});
share|improve this question
    
Since the v 0.9 release of Angular Material it looks like the code above now works, while @jarz code works for v0.8 and below –  dsal1951 May 22 at 16:42

1 Answer 1

up vote 0 down vote accepted

Try changing this:

ng-checked="selection.indexOf(site) > -1" 

to this:

ng-checked="{{selection.indexOf(site) > -1}}" 

Worked for me: http://codepen.io/anon/pen/xbNOmE

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.