最新的Web開發教程
 

AngularJS NG選項指令


通過使用數組的項目填寫一個下拉列表中選擇:

<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>
試一試»

定義和用法

ng-options指令填滿了<選項>在<select>元素。

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


句法

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

由<select>元素的支持。


參數值

Value Description
array expression An expression that selects the specified parts of an array to fill the select element.

Legal expressions:

label for value in array

select as label for value in array

label
group by group for value in array

label
disabled when disable for value in array

label
group by group for value in array track by expression

label
disabled when disable for value in array track by expression

label
for value in array | orderBy expression track by expression