ng-optionsChỉ thị AngularJS


Thí dụ

Điền các tùy chọn trong danh sách thả xuống bằng cách sử dụng các mục của một mả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>

Định nghĩa và Cách sử dụng

Lệnh ng-optionsđiền vào một phần tử <select> với <options>.

Chỉ ng-optionsthị sử dụng một mảng để điền vào danh sách thả xuống. Trong nhiều trường hợp, sử dụng chỉ thị sẽ dễ dàng hơn ng-repeat , nhưng bạn có thể linh hoạt hơn khi sử dụng ng-options chỉ thị.


Cú pháp

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

Được hỗ trợ bởi phần tử <select>.


Giá trị tham số

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
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