关于angularjs:如何使用object设置ng-options默认值?

How to set the ng-options default using object?

如果我将值下拉值设置为对象,则无法按预期工作。

例如 :
HTML:

1
<select ng-model="selected" ng-options="lst as lst.name for lst in list"></select>

JS:

1
2
3
4
5
6
7
8
$scope.list=[{'name':'Person-1','age':23},
  {'name':'Person-2','age':14},
  {'name':'Person-3','age':32},
  {'name':'Person-4','age':34},
  {'name':'Person-5','age':67},
  {'name':'Person-6','age':12}];

   $scope.selected = {'name':'Person-3','age':32};

如果我为$ scope.selected = $ scope.list [0]分配一个值,则工作正常。 但是现在我该如何分配一个对象来选择。

我已经附上了插件链接:http://plnkr.co/edit/WJo0W688s5NDN7kZYOPR?p=preview


这是因为要分配的对象不在$ scope.list中。 但是,它看起来相同,但对象不同。 $scope.list[0]之所以起作用,是因为它指向$ scope.list所具有的同一对象引用。

请参阅更新的插件,以便您了解。

更新

如果有对象,则需要通过比较属性来遍历$scope.list并找到适当对象的索引,然后使用该索引来分配选定的对象。

1
$scope.selected = $scope.list[matchIndex]


1
$scope.selected = $scope.list[0];

这是您设置默认选项的方式