標籤:
1.在 AngularJS 中我們可以使用 ng-option 指令來建立一個下拉式清單,清單項目通過對象和數組迴圈輸出
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>選擇框Select</title> <script src="angular-1.4.1/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","百度","搜狗"]; }); </script></body></html>
運行結果:
用ng-option載入列表:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>選擇框Select</title> <script src="angular-1.4.1/angular.min.js"></script> <script> var app = angular.module(‘myApp‘, []); app.controller(‘customersCtrl‘, function($scope) { $scope.sites = [ {site:"Google",url:"http:www.google.com"}, {site:"百度",url:"http:www.baidu.com"}, {site:"搜狗",url:"http:www.sogou.com"} ]; }); </script></head><body> <div ng-app="myApp" ng-controller="customersCtrl"> <select ng-model="selectedSite" ng-options="x.site for x in sites"> </select> <h1>你選擇的內容如下:</h1> <p>名字:{{selectedSite.site}}</p> <p>網址為:{{selectedSite.url}}</p> </div></body></html>
運行結果:
用ng-repeat載入列表資料也可以但是有局限性,選擇的是一個字串:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>選擇框Select</title> <script src="angular-1.4.1/angular.min.js"></script> <script> var app = angular.module(‘myApp‘, []); app.controller(‘customersCtrl‘, function($scope) { $scope.sites = [ {site:"Google",url:"http:www.google.com"}, {site:"百度",url:"http:www.baidu.com"}, {site:"搜狗",url:"http:www.sogou.com"} ]; }); </script></head><body> <div ng-app="myApp" ng-controller="customersCtrl"> <select ng-model="selectedSite"> <option ng-repeat="x in sites" value="{{x.url}}">{{x.site}}</option> </select> <h1>你選擇的內容是:{{selectedSite}}</h1> </div></body></html>
運行結果:
ng-options使用對象有很大的不同,使用對象作為資料來源, x 為鍵(key), y 為值(value),select控制項ng-options表示控制項的值是什麼,然後ng-model綁定了資料對應的資料來源:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>選擇框Select</title> <script src="angular-1.4.1/angular.min.js"></script> <script> var app = angular.module(‘myApp‘, []); app.controller(‘customersCtrl‘, function($scope) { $scope.sites = { site01 : "Google", site02 : "Runoob", site03 : "Taobao" }; }); </script></head><body> <div ng-app="myApp" ng-controller="customersCtrl"> <select ng-model="selectedSite" ng-options="x for (x, y) in sites"> </select> 你選擇的是:{{selectedSite}} </div></body></html>
運行結果 你選擇的值為在 key-value 對中的 value。
此外value 在 key-value 對中也可以是個對象:選擇的值在 key-value 對的 value 中, 這是它是一個對象:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>選擇框Select</title> <script src="angular-1.4.1/angular.min.js"></script> <script> var app = angular.module(‘myApp‘, []); app.controller(‘customersCtrl‘, 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></head><body> <div ng-app="myApp" ng-controller="customersCtrl"> <select ng-model="selectedCar" ng-options="y.brand for (x, y) in cars"> </select> 你選擇的是:{{selectedCar}} </div></body></html>
運行結果:
AngularJS(6)-選擇框Select