Angular select 不响应 ng-model 更改

Angular select doesn't respond to ng-model changes

我有一个 select 看起来像这样:

{{vm.selected}}
{{vm.items}}
<select ng-options="item.value as item.label for item in vm.items track by item.value" ng-model="vm.selected">
</select>

我的项目数组如下所示:

this.items = [  
  {  
     "label":"Foo Bar",
     "value":"foobar"
  },
  {  
     "label":"Baz Quux",
     "value":"quux"
  }
];

并且项目 vm.selected 是 "foobar"

两个绑定都正确显示在 select 上方,但该项目未 selected。当我在开发人员工具中检查 select 时,我将其作为第一个 selected 项目:

<option value="?" selected="selected"></option>

此外,当我使用 ng-click="vm.selected = 'quux'" 单击此元素时,没有任何改变。 {{vm.selected}} 始终更新。有人知道出了什么问题吗?

根据 official ngOption documentation

,您的 ng-option 表达式将不起作用

引用

but this will not work:

item.subItem as item.label for item in items track by item.id

所以你的

ng-options="item.value as item.label for item in vm.items track by item.value"

不行。

改为使用以下格式的旧表达式:

item as item.label for item in items track by item.id

track by item.value 有问题。如果你删除它,你可以看到在你 select 一个值之后,vm.selected 将正确地显示自己并且 select 工作。

http://codepen.io/jusopi/pen/PbeoWN