最新的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