javascript - How to assign reassign checkbox ng-model on ng-repeat -
please me out. have checkboxes models defined. displaying checkboxes , using model set if checkbox selected or not. below code setting ng-model.
loadvalues(obj) { vm.index = false; vm.create = false; vm.edit = false; vm.delete = false; vm.other = false; var plist = obj.functions; var currentmodule = obj.name; (var = 0; < plist.length; i++) { var currentitem = plist[i]; console.log(currentitem) if (currentitem.search("index") > 0) { vm.index = true; console.log(vm.index); } else if (currentitem.search("create") > 0) { vm.create = true; } else if (currentitem.search("edit") > 0) { vm.edit = true; } else if (currentitem.search("delete") > 0) { vm.delete = true; } else if (currentitem.search("other") > 0) { vm.other = true; } } }
below check boxes.
<tbody> <tr ng-repeat="item in list"> <td> {{item.name}} </td> <td> <input id="index" type="checkbox" ng-model="vm.index" ng-click="editrole(right,item.module,'index')"> </td> <td> <input id="create" type="checkbox" ng-model="vm.create" ng-click="editrole(item.role,'create')"> </td> <td> <input id="edit" type="checkbox" ng-model="vm.edit" ng-click="editrole(item.role,item.module,'edit')"> </td> <td> <input id="delete" type="checkbox" ng-model="vm.delete" ng-click="editrole(item.role,item.module,'delete')"> </td> <td> <input id="other" type="checkbox" ng-model="vm.other" ng-click="editrole(item.role,item.module,'other')"> </td> </tr> </tbody>
the problem assigns same ng-model items in list. have tried find solutions nothing helping. appreciated.
i reading data json file. below example data:
[ {"role":"staff","admins":[{"name":"username","userrights":["useredit","usercreate"] }]
the easiest way use ng-model on checkbox pass abject. code below converts array of items object checkboxes.
i created variable called $scope.userrights
contains of available options.
in html loop though each field displaying name , loop though of userrights
.
the submit button converts object array format received.
html
<div ng:controller="mainctrl"> <button ng-click="submit()">submit</button> <table> <tr ng-repeat="field in fields"> <td ng-bind="field.name"></td> <td ng-repeat="right in userrights"> <label> <input type="checkbox" ng-model="field.userrights[right]" /> {{right}} </label> </td> </tr> </table> <pre ng-bind="fields | json"></pre> </div>
javascript
app.controller('mainctrl', function($scope) { $scope.userrights = ["useredit","usercreate","usersomethingelse"]; $scope.fields = [ {"name":"username","userrights":["useredit","usercreate"]}, {"name":"password","userrights":["useredit"]} ]; // convert array object $scope.fields.foreach(function(field) { var res = {}; field.userrights.foreach(function(right) { res[right] = true; }); field.userrights = res; }); function objectvalues(obj) { var res = []; var keys = object.keys(obj); (var i=0; i<keys.length; i++) { if (obj[keys[i]]) res.push(keys[i]); } return res; } // convert object array $scope.submit = function() { $scope.fields.foreach(function(field) { field.userrights = objectvalues(field.userrights); }); }; });
Comments
Post a Comment