Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS Select和ng选项:我是否可以在仅显示属性时将和对象绑定到ng模型?_Angularjs - Fatal编程技术网

AngularJS Select和ng选项:我是否可以在仅显示属性时将和对象绑定到ng模型?

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

我正在将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>
我的选择如下所示:

  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 ...

现在,您可以尝试组合
键+值
,看看它是否如您所愿。

哦:太粗糙了!谢谢