时间:2021-07-01 10:21:17 帮助过:7人阅读
本文实例为大家分享了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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。