<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        AngularJS中ng-options實現下拉列表的數據綁定方法

        來源:懂視網 責編:小采 時間:2020-11-27 22:10:01
        文檔

        AngularJS中ng-options實現下拉列表的數據綁定方法

        AngularJS中ng-options實現下拉列表的數據綁定方法:下拉列表的簡單使用 ng-option指令使用很簡單,只需要綁定兩個屬性: 一個是ng-model用于獲取選定的值; 另一個是ng-options用于確定下拉列表的元素數組。 <select ng-model=engineer.currentActivity class=form-con
        推薦度:
        導讀AngularJS中ng-options實現下拉列表的數據綁定方法:下拉列表的簡單使用 ng-option指令使用很簡單,只需要綁定兩個屬性: 一個是ng-model用于獲取選定的值; 另一個是ng-options用于確定下拉列表的元素數組。 <select ng-model=engineer.currentActivity class=form-con

        下拉列表的簡單使用

        ng-option指令使用很簡單,只需要綁定兩個屬性:

        一個是ng-model用于獲取選定的值;

        另一個是ng-options用于確定下拉列表的元素數組。

        <select ng-model="engineer.currentActivity" class="form-control" ng-options="act for act in activities"></select>

        上面這條語句就是把選擇的值與engineer.currentActivity進行雙向數據綁定,然后列表中的選項是activities中的每一個值。數據如下:

        $scope.engineer = {
         name: "Dani",
         currentActivity: "Fixing bugs"
         };
         
         $scope.activities =
         [
         "Writing code",
         "Testing code",
         "Fixing bugs",
         "Dancing"
         ];

        運行結果如:

        為了美觀一點,這里引用了bootstrap。

        <html ng-app="myApp">
        <head>
         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
         <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>
         <link rel="stylesheet"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 
         <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
         <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
        </head>
        <body>
         <div ng-controller="EngineeringController" class="container">
         <div class="col-md-12">
         {{engineer.name}} is currently: {{ engineer.currentActivity}}
         </div>
         <div class="col-md-4">
         <label for="name">Choose a new activity:</label>
         <select ng-model="engineer.currentActivity" class="form-control"
         ng-options="act for act in activities"> 
         </select>
         </div>
         </div>
         <script type="text/javascript">
         var myAppModule = angular.module("myApp",[]);
         myAppModule.controller("EngineeringController",["$scope",function($scope){
         $scope.engineer = {
         name: "Dani",
         currentActivity: "Fixing bugs"
         };
         
         $scope.activities =
         [
         "Writing code",
         "Testing code",
         "Fixing bugs",
         "Dancing"
         ];
         }]);
         </script>
        </body>
        </html>

        復雜對象,自定義列表名稱

        有的時候下拉列表并不是單純的字符串數組,可能是json對象,例如:

        $scope.activities =
         [
         { id: 1, type: "Work" , name: "Writing code" },
         { id: 2, type: "Work" , name: "Testing code" },
         { id: 3, type: "Work" , name: "Fixing bugs" },
         { id: 4, type: "Play" , name: "Dancing" }
         ];

        這個時候,綁定的數據就必須是與這里面的格式相同的數據,比如直接復制其中一條:

        $scope.engineer = {
         name: "Dani" ,
         currentActivity: {
         id: 3,
         type: "Work" ,
         name: "Fixing bugs"
         }
         };

        當然也可以直接指定成:

        $scope.engineer = {currentActivity:activities[3]}

        然后在指令中可以循環列表拼接處下拉框的名稱

        <select 
         ng-model = "engineer.currentActivity"
         class="form-control"
         ng-options = "a.name +' (' + a.type + ')' for a in activities" > 
        </select > 

        運行效果如:

        全部的代碼如下:

        <html ng-app="myApp">
        <head>
         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
         <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>
         <link rel="stylesheet"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 
         <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
         <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
        </head>
        <body>
         <div ng-controller="EngineeringController" class="container">
         <div class="col-md-12">
         {{engineer.name}} is currently: {{ engineer.currentActivity}}
         </div>
         <div class="col-md-4">
         <label for="name">Choose a new activity:</label> 
         <select 
         ng-model = "engineer.currentActivity"
         class="form-control"
         ng-options = "a.name +' (' + a.type + ')' for a in activities" > 
         </select > 
         </div>
         </div>
         <script type="text/javascript">
         var myAppModule = angular.module("myApp",[]);
         myAppModule.controller("EngineeringController",["$scope",function($scope){
         $scope.engineer = {
         name: "Dani" ,
         currentActivity: {
         id: 3,
         type: "Work" ,
         name: "Fixing bugs"
         }
         };
         
         $scope.activities =
         [
         { id: 1, type: "Work" , name: "Writing code" },
         { id: 2, type: "Work" , name: "Testing code" },
         { id: 3, type: "Work" , name: "Fixing bugs" },
         { id: 4, type: "Play" , name: "Dancing" }
         ];
         }]);
         </script>
        </body>
        </html>

        實現下拉列表的分組

        其實分組與前面的例子很像,只要把空間中的ng-options的值換成下面:

        <select ng-model = "engineer.currentActivity"
         class="form-control"
         ng-options = "a.name group by a.type for a in activities" > 
        </select >

        添加 group by 就會按照后面的值進行分組

        全部代碼:

        <html ng-app="myApp">
        <head>
         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
         <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>
         <link rel="stylesheet"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 
         <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
         <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
        </head>
        <body>
         <div ng-controller="EngineeringController" class="container">
         <div class="col-md-12">
         {{engineer.name}} is currently: {{ engineer.currentActivity}}
         </div>
         <div class="col-md-4">
         <label for="name">Choose a new activity:</label> 
         <!-- <select 
         ng-model = "engineer.currentActivity"
         class="form-control"
         ng-options = "a.name +' (' + a.type + ')' for a in activities" > 
         </select > -->
         <select ng-model = "engineer.currentActivity"
         class="form-control"
         ng-options = "a.name group by a.type for a in activities" > 
         </select > 
         </div>
         </div>
         <script type="text/javascript">
         var myAppModule = angular.module("myApp",[]);
         myAppModule.controller("EngineeringController",["$scope",function($scope){
         $scope.engineer = {
         name: "Dani" ,
         currentActivity: {
         id: 3,
         type: "Work" ,
         name: "Fixing bugs"
         }
         };
         
         $scope.activities =
         [
         { id: 1, type: "Work" , name: "Writing code" },
         { id: 2, type: "Work" , name: "Testing code" },
         { id: 3, type: "Work" , name: "Fixing bugs" },
         { id: 4, type: "Play" , name: "Dancing" }
         ];
         }]);
         </script>
        </body>
        </html>

        按照id進行標識

        由于之前的ng-model相當于初始的時候給設定了一個值。當你選擇一個下拉列表選項的時候,就會覆蓋掉這個初始值。

        所以更多的時候會使用一個id進行標識,這樣在初始化賦值的時候,只需要設定一個id就可以了。

        $scope.engineer = {
         currentActivityId: 3
         };
         
         $scope.activities =
         [
         { id: 1, type: "Work" , name: "Writing code" },
         { id: 2, type: "Work" , name: "Testing code" },
         { id: 3, type: "Work" , name: "Fixing bugs" },
         { id: 4, type: "Play" , name: "Dancing" }
         ];

        指令可以寫成下面的格式

        <select 
         ng-model = "engineer.currentActivityId"
         class="form-control"
         ng-options = "a.id as a.name group by a.type for a in activities" > 
        </select >

        通過 as 前面的值,就可以確定唯一的一個選項

        全部代碼如下:

        <html ng-app="myApp">
        <head>
         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
         <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>
         <link rel="stylesheet"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 
         <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
         <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
        </head>
        <body>
         <div ng-controller="EngineeringController" class="container">
         <div class="col-md-12">
         current is: {{ engineer.currentActivityId}}
         </div>
         <div class="col-md-4">
         <label for="name">Choose a new activity:</label> 
         <select 
         ng-model = "engineer.currentActivityId"
         class="form-control"
         ng-options = "a.id as a.name group by a.type for a in activities" > 
         </select > 
         </div>
         </div>
         <script type="text/javascript">
         var myAppModule = angular.module("myApp",[]);
         myAppModule.controller("EngineeringController",["$scope",function($scope){
         $scope.engineer = {
         currentActivityId: 3
         };
         
         $scope.activities =
         [
         { id: 1, type: "Work" , name: "Writing code" },
         { id: 2, type: "Work" , name: "Testing code" },
         { id: 3, type: "Work" , name: "Fixing bugs" },
         { id: 4, type: "Play" , name: "Dancing" }
         ];
         }]);
         </script>
        </body>
        </html>

        以上這篇AngularJS中ng-options實現下拉列表的數據綁定方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        AngularJS中ng-options實現下拉列表的數據綁定方法

        AngularJS中ng-options實現下拉列表的數據綁定方法:下拉列表的簡單使用 ng-option指令使用很簡單,只需要綁定兩個屬性: 一個是ng-model用于獲取選定的值; 另一個是ng-options用于確定下拉列表的元素數組。 <select ng-model=engineer.currentActivity class=form-con
        推薦度:
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲成人网在线观看| 亚洲成AV人片在线观看| 免费国产黄线在线观看| 欧洲一级毛片免费| 国产成人无码a区在线观看视频免费 | 亚洲黄页网在线观看| 亚洲AV无码一区二区大桥未久| 色www免费视频| 99在线免费观看视频| 成人午夜性A级毛片免费| 国产亚洲成AV人片在线观黄桃| 亚洲一区二区三区丝袜| 中文字幕乱码系列免费| 最近2019中文字幕mv免费看| 亚洲性色精品一区二区在线| 日本无吗免费一二区| 久久精品亚洲中文字幕无码麻豆| 国产精品亚洲色婷婷99久久精品| 亚洲三级在线免费观看| 77777午夜亚洲| 91麻豆国产免费观看| 超清首页国产亚洲丝袜| 亚洲一卡二卡三卡| 91人成网站色www免费下载| 亚洲91精品麻豆国产系列在线| 成人av免费电影| 亚洲国产精品久久丫| a毛片久久免费观看| 久久精品国产免费观看| 中文字幕乱码亚洲精品一区| 亚洲国产精品一区二区第一页免 | 亚洲欧洲AV无码专区| 亚洲网站免费观看| 亚洲AV天天做在线观看| 男男gay做爽爽的视频免费| 亚洲色大成网站www永久一区| 性生大片视频免费观看一级| 最新仑乱免费视频| 精品乱子伦一区二区三区高清免费播放 | 好吊色永久免费视频大全| 亚洲沟沟美女亚洲沟沟|