AngularJS学习篇(十)

AngularJS Select(选择框)

应用 ng-options 创建选择框

于 AngularJS
中我们得使 ng-option 指令来创造一个下拉列表,列表项通过对象以及数组循环输出,如下实例:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="angular-1.6.3/angular.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
   <select ng-init="selectedName = names[0]" 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','baidu','tengxun'];
    })
</script>
</body>
</html>

ng-init 初始值,ng-options=”x for x in names”读取数组

用 ng-repeat 创建选择框

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="angular-1.6.3/angular.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
   <select>
       <option ng-repeat="x in names">{{x}}</option>
   </select>
</div>
<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl',function ($scope) {
        $scope.names=['google','baidu','tengxun'];
    })
</script>
</body>
</html>

ng-repeat 指令是由此数组来循环 HTML
代码来创造下拉列表,但 ng-options 指令更称创建下拉列表,它发生以下优势:

使用 ng-options 的选择是一个对象, ng-repeat 是一个字符串。当选择值是一个对象时,我们就算好获取更多信息,应用为再也灵活。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="angular-1.6.3/angular.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
   <select ng-model="selectedSite">
       <option ng-repeat="x in names" value="{{x.url}}">{{x.site}}</option>
   </select>

    <h1>你选择的是: {{selectedSite}}</h1>   
</div>
<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl',function ($scope) {
        $scope.names=[ {site : "Google", url : "http://www.google.com"},
            {site : "Runoob", url : "http://www.runoob.com"},
            {site : "Taobao", url : "http://www.taobao.com"}];
    })
</script>
</body>
</html>

您选择的凡: http://www.runoob.com—-&gt;字符串

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="angular-1.6.3/angular.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
   <select ng-model="selectedSite" ng-options="x.site for x in names">
   </select>

    <h1>你选择的是: {{selectedSite}}</h1>
</div>
<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl',function ($scope) {
        $scope.names=[ {site : "Google", url : "http://www.google.com"},
            {site : "Runoob", url : "http://www.runoob.com"},
            {site : "Taobao", url : "http://www.taobao.com"}];
    })
</script>
</body>
</html>

汝选的是: {“site”:”Google”,”url”:”http://www.google.com"}—-&gt;对象

 

数源为对象

采取对象作为数据源, x 为键(key), y 为值(value):

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="angular-1.6.3/angular.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
   <select ng-model="selectedSite" ng-options="x for (x,y) in names">
   </select>

    <h1>你选择的是: {{selectedSite}}</h1>
</div>
<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl',function ($scope) {
        $scope.names= {site1 : "Google", site2 : "http://www.google.com"};
    })
</script>
</body>
</html>

公选的是: Google

你拣的价为于 key-value 对中的 value

 

value 在 key-value 对面临也得是单对象:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="angular-1.6.3/angular.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
   <select ng-model="selectedSite" ng-options="x for (x,y) in names">
   </select>

    <h1>你选择的是: {{selectedSite}}</h1>
</div>
<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl',function ($scope) {
        $scope.names= {car01 : {brand : "Ford", model : "Mustang", color : "red"},
            car02 : {brand : "Fiat", model : "500", color : "white"},
            car03 : {brand : "Volvo", model : "XC90", color : "black"}};
    })
</script>
</body>
</html>

卿挑选的是: {“brand”:”Ford”,”model”:”Mustang”,”color”:”red”}

 

以下拉菜单也堪无行使 key-value 对中的 key ,
直接运用对象的习性,但是选择还是value:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="angular-1.6.3/angular.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
   <select ng-model="selectedSite" ng-options="y.brand for (x,y) in names">
   </select>

    <h1>你选择的是: {{selectedSite}}</h1>
</div>
<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl',function ($scope) {
        $scope.names= {car01 : {brand : "Ford", model : "Mustang", color : "red"},
            car02 : {brand : "Fiat", model : "500", color : "white"},
            car03 : {brand : "Volvo", model : "XC90", color : "black"}};
    })
</script>
</body>
</html>

卿挑选的凡: {“brand”:”Ford”,”model”:”Mustang”,”color”:”red”}

 

相关文章