角度ng-options,具有按对象id预选的值

时间:2016-04-15 09:55:30

标签: javascript angularjs ng-repeat ng-options

以下select是否可以拥有pre-selected分会?

<select ng-model="c.pilot.clubIdx" ng-options="club.idx as club.name for club in c.clubs track by club.idx">

c.pilot.clubIdx = 2
c.clubs = {{idx:1,name:“club1”},{idx:2,name:“club2”}}

选择菜单应显示“club2”。

由于俱乐部存储在数据库中并且可以更改,我希望将它们仅references保留为pilots

已解决:通过解决问题来删除曲目。

4 个答案:

答案 0 :(得分:0)

<select ng-model="c.pilot.clubIdx" ng-options="club.idx as club.name for club   in c.clubs track by club.idx" ng-selected="(club.idx == 2)?true:false">

像这样添加ng-selected。应该预先选择

答案 1 :(得分:0)

使用下划线/ lodash在控制器中使用以下行:

var club =_.where(c.clubs, {name:"club2"});
$scope.c.pilot.clubIdx = club.idx;

答案 2 :(得分:0)

在控制器中将select的ng-model设置为您想要选择的选项的值。例如:$scope.c.pilot.clubIdx = 2$scope.c.pilot.clubIdx = $scope.c.clubs[1].idx

答案 3 :(得分:0)

removing the track by clause solves the problem. i dont fully understand why, but who knows. angular doc for ngOptions