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
<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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With