<!-- $watch:持續監聽數據上的變化,更新界面 -->
<!DOCTYPE html>
<html lang="en" ng-app="myApp" ng-controller="myCtrl">
<head>
<meta charset="utf-8">
<script src="js/angular.js"></script>
</head>
<body>
使用ng-options
<select ng-model=names[0] ng-options="x for x in names">
</select><br>
使用ng-repeat
<select>
<option ng-repeat="x in names">{{x}}</option>
</select><br><br>
區別<br>
ng-options更適合來做下拉菜單<br>
爲什麼這麼說?<br><br><br>
<div style="color: red">使用ng-repeat操作數組</div><br>
<select ng-model="selectedSite">
<option ng-repeat="x in sites" value="{{x.url}}">{{x.site}}</option>
</select><br>
<h1>你選擇的是: {{selectedSite}}</h1><br>
<div style="color: red">使用ng-options操作數組</div><br>
<select ng-model="selectedSite2" ng-options="x.site for x in sites">
</select><br>
<h1>你選擇的是: {{selectedSite2.site}}</h1><br>
<p>網址爲: {{selectedSite2.url}}</p><br><br>
看得出,ng-options操作的是對象 而ng-repeat操作的是字符串
當選擇值是一個對象時,我們就可以獲取更多信息,應用也更靈活。<br><br><br><br>
<div style="color: red">使用ng-options來操作對象</div>
<select ng-model="selectedSite3" ng-options="x for (x, y) in sites2">
</select><br>
<h1>你選擇的值是: {{selectedSite3}}</h1><br><br><br><br>
<p>選擇一輛車:</p><br>
<select ng-model="selectedCar" ng-options="x for (x, y) in cars">
</select><br>
<h1>你選擇的是: {{selectedCar.brand}}</h1><br>
<h2>模型: {{selectedCar.model}}</h2><br>
<h3>顏色: {{selectedCar.color}}</h3><br>
<p>注意選中的值是一個對象。</p>
</body>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.names = ["Google", "Runoob", "Taobao"];
$scope.sites = [
{site : "Google", url : "http://www.google.com"},
{site : "Runoob", url : "http://www.runoob.com"},
{site : "Taobao", url : "http://www.taobao.com"}
];
$scope.sites2 = {
site01 : "Google",
site02 : "Runoob",
site03 : "Taobao"
};
$scope.cars = {
car01 : {brand : "Ford", model : "Mustang", color : "red"},
car02 : {brand : "Fiat", model : "500", color : "white"},
car03 : {brand : "Volvo", model : "XC90", color : "black"}
}
});
</script>
</html>
21-service之select下拉菜單
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.