1

I have a simple dropdown:

 {{x}}            
 <select ng-model="x" ng-options="k for k in [1,2,3]"></select>

easy: {{x}} will be replaced with the selected value 1, 2 or 3.

But what if my data looks like this?

 <select ng-model="x" ng-options="k.a for k in [{a:1},{a:2},{a:3}]">

I want the ones, twos and threes depending on selection but I usually get "{a:1}".

Hm.. this suggests that maybe I won't get what I want. Ideas?

4

1 回答 1

3

你只少了一件。

如果您查看 的文档ng-options,则理解表达式可以采用多种形式。

您拥有的表格是label for value in array. 使用这种形式,当您选择一个选项时,它会为您提供数组项的整个值,在您的情况下类似于{a: 1}.

您可以使用的其他形式之一是select as label for value in array. 您在 from 中添加一个额外的表达式,表达式的结果将绑定到您的模型。

所以像:

ng-options="k.a as k.a for k in [...]"

然后您将获得a所选数组项的属性值。

这是给你的小提琴

于 2013-02-19T22:24:35.450 回答