本文實例為大家分享了service之select下拉菜單效果的具體代碼,供大家參考,具體內容如下
<!-- $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>
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com