AngularJS Select和ng选项:我是否可以在仅显示属性时将和对象绑定到ng模型?
我正在将select元素绑定到如下所示的数据:AngularJS Select和ng选项:我是否可以在仅显示属性时将和对象绑定到ng模型?,angularjs,Angularjs,我正在将select元素绑定到如下所示的数据: var gearConditions = [ { id: 1, value: 'New' }, { id: 2, value: 'Like New' }, { id: 3, value: 'Excellent' }, { id: 4, value: 'Good' }, { id: 5, value: 'Fair' }, { id: 6, value: 'Very Used' }, { id
var gearConditions = [
{ id: 1, value: 'New' },
{ id: 2, value: 'Like New' },
{ id: 3, value: 'Excellent' },
{ id: 4, value: 'Good' },
{ id: 5, value: 'Fair' },
{ id: 6, value: 'Very Used' },
{ id: 7, value: 'Other' }
];
<select name="condition"
class="form-control"
ng-model="postModel.condition"
ng-options="condition.id as condition.value for condition in gearConditions">
</select>
我的选择如下所示:
var gearConditions = [
{ id: 1, value: 'New' },
{ id: 2, value: 'Like New' },
{ id: 3, value: 'Excellent' },
{ id: 4, value: 'Good' },
{ id: 5, value: 'Fair' },
{ id: 6, value: 'Very Used' },
{ id: 7, value: 'Other' }
];
<select name="condition"
class="form-control"
ng-model="postModel.condition"
ng-options="condition.id as condition.value for condition in gearConditions">
</select>
这会将condition.id绑定到ng模型,并将condition.value显示为选择选项。我的问题是:我是否可以保留当前行为,但将整个对象绑定到ng模型
例如,如果选择了第一个选项,那么模型值将是{id:1,值:'New'},而不仅仅是1
这是可能的,还是我需要使用ng changed来实现这一点
谢谢 是,将
条件.id
更改为条件
:
ng-options="condition as condition.value for condition in gearConditions"
我想你也尝试过类似的方法:
// I took this from my code
ng-options="key as value for (key, value) in ...
现在,您可以尝试组合
键+值
,看看它是否如您所愿。哦:太粗糙了!谢谢