javascript - Angular ng-repeat with bootstrap input checkbox with dynamic ng-model using ng-true-value -


this problem little tricky me explain i'll best. i'm trying implement angular checkbox ng-true-value correctly dynamic ng-model.

<div ng-app="myapp"> <div ng-controller="myctrl">     <div ng-repeat="asset in asset">          <h5>{{ asset.title }} categories are...{{ asset.category[0] + " , " + asset.category[1] }}</h5>          <div class="form-group">             <label>business</label>             <div class="checkbox" ng-repeat="category in categoryfactory.business">                 <input type="checkbox" id="{{category}}" ng-true-value="{{'category'}}" ng-model="asset.category[$index]">                 <label for="{{category}}">{{category}}</label>             </div>         </div>         <div class="form-group">             <label>personal</label>             <div class="checkbox" ng-repeat="category in categoryfactory.personal">                 <input type="checkbox" id="{{category}}" ng-true-value="{{'category'}}" ng-model="asset.category[$index]">                 <label for="{{category}}">{{category}}</label>             </div>         </div>     </div> </div> 

my end goal when load asset (find below...) display picture here my end goal, correct checkboxes checked. business , personal categories derived factory , each form-group checkbox's ng-repeat list in business / personal in factory.

$scope.asset = [{   title: "sales agreement",   category: ["finance-admin", "running-a-business"] }]; 

my issue having correctly implement ng-model inside checkbox. since each asset can have 1 or more category needs dynamic. have tried using $index make ng-model dynamic...

ng-model="asset.category[$index]" 

but seems not work have hoped. missing easy , appreciated.

thanks! way here jsfiddle

i used ng-checked, can put function attribute value, ng-repeat evaluate function in each loop.

<input type="checkbox" id="{{category}}" ng-checked="iscategory(category)" >  $scope.iscategory = function(category){    return  $scope.asset[0].category.indexof(category) >= 0; } 

in iscategory() checking indices of category in asset.category array, if finds true.

https://jsfiddle.net/pg5dqgr9/8/


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -