標籤:
AngularJS 可以使用數組或對象建立一個下拉式清單選項。
使用 ng-option 建立選擇框
在 AngularJS 中我們可以使用 ng-option 指令來建立一個下拉式清單,清單項目通過對象和數組迴圈輸出,如下執行個體:
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="myCtrl"><select ng-model="selectedName" ng-options="x for x in names"></select></div><script>var app = angular.module(‘myApp‘, []);app.controller(‘myCtrl‘, function($scope) { $scope.names = ["Google", "Runoob", "Taobao"];});</script><p>該執行個體示範了 ng-options 指令的使用。</p></body></html>
ng-option與ng-repeat:
我們也可以使用ng-repeat 指令來建立下拉式清單:
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="myCtrl"><select><option ng-repeat="x in names">{{x}}</option></select></div><script>var app = angular.module(‘myApp‘, []);app.controller(‘myCtrl‘, function($scope) { $scope.names = ["Google", "Runoob", "Taobao"];});</script><p>該執行個體示範了使用 ng-repeat 指令來建立下拉式清單。</p></body></html>
ng-option與ng-repeat用哪一個來做選擇框更好呢?我們傾向於使用ng-option,因為ng-repeat有局限性,選擇的值是一個字串,而使用 ng-options 指令,選擇的值是一個對象,當選擇值是一個對象時,我們就可以擷取更多資訊,應用也更靈活。
以下為二者的執行個體對比:
假設我們使用如下資料:
$scope.sites = [ {site : "Google", url : "http://www.google.com"}, {site : "Runoob", url : "http://www.runoob.com"}, {site : "Taobao", url : "http://www.taobao.com"}];
使用 ng-repeat:
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="myCtrl"><p>選擇網站:</p><select ng-model="selectedSite"><option ng-repeat="x in sites" value="{{x.url}}">{{x.site}}</option></select><h1>你選擇的是: {{selectedSite}}</h1></div><script>var app = angular.module(‘myApp‘, []);app.controller(‘myCtrl‘, function($scope) { $scope.sites = [ {site : "Google", url : "http://www.google.com"}, {site : "Runoob", url : "http://www.runoob.com"}, {site : "Taobao", url : "http://www.taobao.com"}];});</script><p>該執行個體示範了使用 ng-repeat 指令來建立下拉式清單,選中的值是一個字串。</p></body></html>
使用 ng-options:
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="myCtrl"><p>選擇網站:</p><select ng-model="selectedSite" ng-options="x.site for x in sites"></select><h1>你選擇的是: {{selectedSite.site}}</h1><p>網址為: {{selectedSite.url}}</p></div><script>var app = angular.module(‘myApp‘, []);app.controller(‘myCtrl‘, function($scope) { $scope.sites = [ {site : "Google", url : "http://www.google.com"}, {site : "Runoob", url : "http://www.runoob.com"}, {site : "Taobao", url : "http://www.taobao.com"}];});</script><p>該執行個體示範了使用 ng-options 指令來建立下拉式清單,選中的值是一個對象。</p></body></html>
前面執行個體我們使用了數組作為資料來源,以下我們將資料對象作為資料來源。
$scope.sites = { site01 : "Google", site02 : "Runoob", site03 : "Taobao"};
ng-options 使用對象有很大的不同,使用對象作為資料來源, x 為鍵(key), y 為值(value):
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="myCtrl"><p>選擇的網站是:</p><select ng-model="selectedSite" ng-options="x for (x, y) in sites"></select><h1>你選擇的值是: {{selectedSite}}</h1></div><p>該執行個體示範了使用對象作為建立下拉式清單。</p><script>var app = angular.module(‘myApp‘, []);app.controller(‘myCtrl‘, function($scope) { $scope.sites = { site01 : "Google", site02 : "Runoob", site03 : "Taobao"};});</script></body></html>
你選擇的值為在 key-value 對中的 value。
value 在 key-value 對中也可以是個對象:
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="myCtrl"><p>選擇一輛車:</p><select ng-model="selectedCar" ng-options="x for (x, y) in cars"></select><h1>你選擇的是: {{selectedCar.brand}}</h1><h2>模型: {{selectedCar.model}}</h2><h3>顏色: {{selectedCar.color}}</h3><p>注意選中的值是一個對象。</p></div><script>var app = angular.module(‘myApp‘, []);app.controller(‘myCtrl‘, function($scope) { $scope.cars = { car01 : {brand : "Ford", model : "Mustang", color : "red"}, car02 : {brand : "Fiat", model : "500", color : "white"}, car03 : {brand : "Volvo", model : "XC90", color : "black"} }});</script></body></html>
在下拉式功能表也可以不使用 key-value 對中的 key , 直接使用對象的屬性:
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="myCtrl"><p>選擇一輛車:</p><select ng-model="selectedCar" ng-options="y.brand for (x, y) in cars"></select><p>你選擇的是: {{selectedCar.brand}}</p><p>型號為: {{selectedCar.model}}</p><p>顏色為: {{selectedCar.color}}</p><p>下拉式清單中的選項也可以是對象的屬性。</p></div><script>var app = angular.module(‘myApp‘, []);app.controller(‘myCtrl‘, function($scope) { $scope.cars = { car01 : {brand : "Ford", model : "Mustang", color : "red"}, car02 : {brand : "Fiat", model : "500", color : "white"}, car03 : {brand : "Volvo", model : "XC90", color : "black"} }});</script></body></html>
AngularJS Select(選擇框)