AngularJS ng-options 指令

定義和用法

ng-options 指令用 <options> 填充 lt;select> 元素。

ng-options 指令使用數組來填充下拉列表。在許多情況下,使用 ng-repeat 指令會更容易,但使用 ng-options 指令時您有更大的靈活性。

實例

通過使用數組中的項目在下拉列表中填充選項:

<div ng-app="myApp" ng-controller="myCtrl">
<select ng-model="selectedName" ng-options="item for item in names"></select>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.names = ["Emil", "Tobias", "Linus"];
});
</script>

親自試一試

語法

<select ng-options="array expression"></select>

<select> 元素支持。

參數

參數 描述
array expression

表達式,它選擇數組的指定部分來填充 select 元素。

合法的表達式:

label for value in array

select as label for value in array

label group by group for value in array

label disable when disable for value in array

label group by group for value in array track by expression

label disable when disable for value in array track by expression

label for value in array | orderBy expression track by expression