Tharindu Senanayake
Tharindu Senanayake

Reputation: 285

Using multiple ng-models in a single ng-repeat

I'm trying to get multiple values selected form a checkbox list generated using ng-repeat in anguler

<div ng-repeat="item in items">

<input type="checkbox" ng-model="model.ID" ng-true-value="'{{item.ID}}'" ng-false-value="''"/>{{item.Name}}
</div>

But what I want is to have different ng-models for different check boxes. How can I do that. Is there a way to do what ng-repeat does with out using ng-repeat

Upvotes: 1

Views: 2489

Answers (2)

byteC0de
byteC0de

Reputation: 5273

In Angular one checkbox is linked with one model, but in practice we usually want one model to store array of checked values from several checkboxes. Checklist-model solves that task without additional code in controller. examples

Upvotes: 1

aseferov
aseferov

Reputation: 6403

use $index as key

<div ng-repeat="item in items">

<input type="checkbox" ng-model="model[$index].ID"/>{{item.Name}}
</div>

or use item unique id:

<div ng-repeat="item in items">

<input type="checkbox" ng-model="model[item.ID]"/>{{item.Name}}
</div>

Upvotes: 1

Related Questions