如何在选择列表Angular JS中选择第一个元素?

时间:2015-09-26 19:35:02

标签: angularjs

我选择了包含选项的列表:

  <select ng-model="yr[$index]" ng-selected="year.id == 'I'" ng-options="year.id as year.id for year in years">
    </select>

如何在选择列表中选择第一个元素,其中element是string? 我的代码是:

http://plnkr.co/edit/U4Fxh0JSxxtwhJWT9opG?p=preview

2 个答案:

答案 0 :(得分:2)

您可以编写另一个名为currentYear的变量,如下所示:

$scope.currentYear = $scope.years[0];

并选择以下内容:

 <select ng-model="currentYear" ng-options="year.id disable when (year.id < yr[$index]) for year in years">
</select>

更新后

要显示该值,您可以将select添加如下:

<select ng-model="currentYear.value" ng-options="year.value as year.value for year in years">
</select>

答案 1 :(得分:1)

尝试使用:ng-init =“something = options [0]”

You can read about it here