daremachine daremachine - 4 months ago 37
AngularJS Question

angular ng-repeat checkbox from resource and set checked

i have resource in my controller and populate checkboxes with ng-repeat
next thing i know in controller which are to be checked

code will looks like..

$scope.data = resource.query();

var arr_to_be_checked = [1,3,5];


in other controller i use this..

$scope.data = [ { Type: 1, checked: true, Title: 'Item A'},
{ Type: 2, checked: false, Title: 'Item B'}];


and its working fine but i need apply this on resource and ng-repeat because i need more flexible

i find any soulution but without point. Pls can you help me anyone?

My question is: How i can "override" objects in resource with 'checked:true' or set any checkbox as checked.

Thank you so much for help or any idea

Have a nice day



Answer
<ul>
  <li ng-repeat="item in data">
    <input type="checkbox" ng-checked="item.checked"> {{item.Title}}
  </li>
</ul>
<button ng-click="checkItems()">Do</button> 

.

$scope.checkItems = function () {
  var i;
  for (i = 0; i < arr_to_be_checked.length; i++) {
    data[arr_to_be_checked[i]].checked = true;
  } 
};

PS: Please be really consistent on your naming conventions, javascript uses a camelCase naming convention, no underscores. Only constructor functions are named PascalCase

Related API Documentation:
ng-click
ng-checked

Comments