这篇文章主要介绍了AngularJS怎么实现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 >使用ng-repeat操作数组</div><br>
<select ng-model="selectedSite">
<option ng-repeat="x in sites" value="{{x.url}}">{{x.site}}</option>
</select><br>
<h2>你选择的是: {{selectedSite}}</h2><br>
<div >使用ng-options操作数组</div><br>
<select ng-model="selectedSite2" ng-options="x.site for x in sites">
</select><br>
<h2>你选择的是: {{selectedSite2.site}}</h2><br>
<p>网址为: {{selectedSite2.url}}</p><br><br>
看得出,ng-options操作的是对象 而ng-repeat操作的是字符串
当选择值是一个对象时,我们就可以获取更多信息,应用也更灵活。<br><br><br><br>
<div >使用ng-options来操作对象</div>
<select ng-model="selectedSite3" ng-options="x for (x, y) in sites2">
</select><br>
<h2>你选择的值是: {{selectedSite3}}</h2><br><br><br><br>
<p>选择一辆车:</p><br>
<select ng-model="selectedCar" ng-options="x for (x, y) in cars">
</select><br>
<h2>你选择的是: {{selectedCar.brand}}</h2><br>
<h3>模型: {{selectedCar.model}}</h3><br>
<h4>颜色: {{selectedCar.color}}</h4><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>
感谢你能够认真阅读完这篇文章,希望小编分享的“AngularJS怎么实现service之select下拉菜单效果”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网行业资讯频道,更多相关知识等着你来学习!