欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AngularJS中ng-options實(shí)現(xiàn)下拉列表的數(shù)據(jù)綁定方法

 更新時(shí)間:2018年08月13日 14:44:01   作者:小飛鶴  
今天小編就為大家分享一篇AngularJS中ng-options實(shí)現(xiàn)下拉列表的數(shù)據(jù)綁定方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

下拉列表的簡單使用

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

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

另一個是ng-options用于確定下拉列表的元素?cái)?shù)組。

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

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

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

運(yùn)行結(jié)果如:

為了美觀一點(diǎn),這里引用了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>

復(fù)雜對象,自定義列表名稱

有的時(shí)候下拉列表并不是單純的字符串?dāng)?shù)組,可能是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" }
  ];

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

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

當(dāng)然也可以直接指定成:

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

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

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

運(yùn)行效果如:

全部的代碼如下:

<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>

實(shí)現(xiàn)下拉列表的分組

其實(shí)分組與前面的例子很像,只要把空間中的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 就會按照后面的值進(jìn)行分組

全部代碼:

<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進(jìn)行標(biāo)識

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

所以更多的時(shí)候會使用一個id進(jìn)行標(biāo)識,這樣在初始化賦值的時(shí)候,只需要設(shè)定一個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 前面的值,就可以確定唯一的一個選項(xiàng)

全部代碼如下:

<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實(shí)現(xiàn)下拉列表的數(shù)據(jù)綁定方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • angular強(qiáng)制更新ui視圖的實(shí)現(xiàn)方法

    angular強(qiáng)制更新ui視圖的實(shí)現(xiàn)方法

    這篇文章主要介紹了angular強(qiáng)制更新ui視圖的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Angular2平滑升級到Angular4的步驟詳解

    Angular2平滑升級到Angular4的步驟詳解

    最近Angular項(xiàng)目組終于發(fā)布了新版——正式版 Angular 4.0.0。所以想著就來嘗試下升級,記錄下整個升級過程分享給大家,所以這篇文章主要介紹了Angular2升級到Angular4的詳細(xì)步驟,需要的朋友可以參考下。
    2017-03-03
  • AngularJS中監(jiān)視Scope變量以及外部調(diào)用Scope方法

    AngularJS中監(jiān)視Scope變量以及外部調(diào)用Scope方法

    在AngularJS中,有時(shí)候需要監(jiān)視Scope中的某個變量,因?yàn)樽兞康母淖儠绊懸恍┙缑嬖氐娘@示,接下來通過本文給大家介紹AngularJS中監(jiān)視Scope變量以及外部調(diào)用Scope方法,需要的朋友參考下吧
    2016-01-01
  • 9種改善AngularJS性能的方法

    9種改善AngularJS性能的方法

    這篇文章主要為大家詳細(xì)介紹了9種改善AngularJS性能的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • 使用RxJS更優(yōu)雅地進(jìn)行定時(shí)請求詳析

    使用RxJS更優(yōu)雅地進(jìn)行定時(shí)請求詳析

    這篇文章主要給大家介紹了關(guān)于如何使用RxJS更優(yōu)雅地進(jìn)行定時(shí)請求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用RxJS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • 淺析Angular2子模塊以及異步加載

    淺析Angular2子模塊以及異步加載

    本篇文章主要介紹了淺析Angular2子模塊以及異步加載,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Angular自定義組件實(shí)現(xiàn)數(shù)據(jù)雙向數(shù)據(jù)綁定的實(shí)例

    Angular自定義組件實(shí)現(xiàn)數(shù)據(jù)雙向數(shù)據(jù)綁定的實(shí)例

    下面小編就為大家分享一篇Angular自定義組件實(shí)現(xiàn)數(shù)據(jù)雙向數(shù)據(jù)綁定的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • Angular.JS中指令ng-if、ng-show/ng-hide和ng-switch的使用教程

    Angular.JS中指令ng-if、ng-show/ng-hide和ng-switch的使用教程

    在Angular的原生指令中有這幾個指令用來控制元素的展示與否,ng-show/ng-hide/ng-if和ng-switch。在angular性能優(yōu)化中,我們也常常會用到它。這篇文章主要給大家介紹了在Angular.JS中指令ng-if、ng-show/ng-hide和ng-switch的使用教程,需要的朋友可以參考。
    2017-05-05
  • AngularJS實(shí)現(xiàn)頁面跳轉(zhuǎn)后自動彈出對話框?qū)嵗a

    AngularJS實(shí)現(xiàn)頁面跳轉(zhuǎn)后自動彈出對話框?qū)嵗a

    這篇文章主要介紹了AngularJS實(shí)現(xiàn)頁面跳轉(zhuǎn)后自動彈出對話框?qū)嵗a,然后在文章下面給大家介紹了angularjs頁面加載后自動彈窗的實(shí)例代碼,感興趣的朋友參考下吧
    2017-08-08
  • AngularJS入門教程之靜態(tài)模板詳解

    AngularJS入門教程之靜態(tài)模板詳解

    本文主要介紹AngularJS 靜態(tài)模板,這里整理了相關(guān)的基礎(chǔ)資料,會幫助大家學(xué)習(xí)基礎(chǔ)的AngularJS的基礎(chǔ)知識,有需要的小伙伴可以參考下
    2016-08-08

最新評論