如何将ng选项的索引作为angularJs中的值传递给模型

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

本文关键字:angularJs 值传 模型 ng 选项 索引      更新时间:2023-09-26

在我的代码中,我选择了一个当前如下的代码:

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

最初的doseUnitChoices看起来是这样的:

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

但现在它不再具有id值,所以我需要将它们在阵列中的位置的index而不是id作为choice.id传递给ng-model

我将ng选项改写为:

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

但是CCD_ 7似乎保持未定义。那么,在doseUnitChoices 中是否有将模型值作为所选index的方法?

尝试这个

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