Angular ng-repeat checkbox from resource and checkbox checked

I have a resource in my controller and the following ng-repeat checkboxes are filled with checkboxes that I know in the controller that should be checked.

the code will look like.

$scope.data = resource.query(); var arr_to_be_checked = [1,3,5]; 

in another controller i use this.

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

and its working mode, but I need to apply this on the resource and ng-repeat, because I need more flexible

I find any soul, but without meaning. Pls can you help me?

My question is: how can I "override" objects in a resource using "checked: true" or check any box.

Thank you so much for your help or any idea.
Have a nice day


+4
source share
3 answers
 <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 very consistent in your naming conventions, javascript uses the camelCase naming camelCase , without underlining. Only design functions are called PascalCase

Related API documentation:
ng-click
ng-checked

+12
source

Another solution:

 <div ng-controller="MainCtrl"> <label ng-repeat="(color,enabled) in colors"> <input type="checkbox" ng-model="colors[color]" /> {{color}} </label> <p>colors: {{colors}}</p> <script> var app = angular.module('plunker', []); app.controller('MainCtrl', function($scope){ $scope.colors = {Blue: true, Orange: true}; }); 

http://plnkr.co/edit/U4VD61?p=preview

+7
source

DECISION

I am looking at a solution from fastreload and is modifying like this.

 var arr_checked_items = [1,2,5] // this checkboxes will be set as checked <ul> <li ng-repeat="item in data"> <input type="checkbox" ng-checked="checkItem(item.id)"> {{item.Title}} </li> </ul> 

.

 $scope.checkItem = function (id) { var checked = false; for(var i=0; i<= arr_to_be_checked.length; i++) { if(id == arr_to_be_checked[i]) { checked = true; } } return checked; }; 

IT WORKS PERFECTLY !!! when loading without pressing buttons

+1
source

All Articles