Andrew Font
Andrew Font

Reputation: 1285

How can I pass the index of an ng-option to the model as a value in angularJs?

In my code I have a select with code that currently looks like this:

data-ng-options="choice.id as choice.singular for choice in doseUnitChoices"
data-ng-model="choiceId"

originally doseUnitChoices used to look like this:

[
 {id:1, singular: 'string'},
 {id:2, singular: 'string2'},
 {id:3, singular: 'string3'}
]

But now it no longer has the id value any more so I need to pass the index of their position in the array instead of the id as choice.id to the ng-model

I rewrote my ng-option to this:

data-ng-options="choice as choice.singular for choice in doseUnitChoices track by $index"

but the ng-model appears to stay undefined. So is there anyway to get the model value to be the index of the choice within doseUnitChoices

Upvotes: 0

Views: 338

Answers (1)

aseferov
aseferov

Reputation: 6393

try this

data-ng-options="index as choice.singular for (index,choice) in doseUnitChoices"

Upvotes: 1

Related Questions