AngularJS(6)-選擇框Select

來源:互聯網
上載者:User

標籤:

1.在 AngularJS 中我們可以使用 ng-option 指令來建立一個下拉式清單,清單項目通過對象和數組迴圈輸出

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>選擇框Select</title>    <script src="angular-1.4.1/angular.min.js"></script></head><body>   <div ng-app="myApp" ng-controller="myCtrl">      <select 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","百度","搜狗"];       });   </script></body></html>

運行結果:  

用ng-option載入列表:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>選擇框Select</title>    <script src="angular-1.4.1/angular.min.js"></script>    <script>        var app = angular.module(‘myApp‘, []);        app.controller(‘customersCtrl‘, function($scope) {            $scope.sites = [                {site:"Google",url:"http:www.google.com"},                {site:"百度",url:"http:www.baidu.com"},                {site:"搜狗",url:"http:www.sogou.com"}            ];        });    </script></head><body>  <div ng-app="myApp" ng-controller="customersCtrl">      <select ng-model="selectedSite" ng-options="x.site for x in sites">      </select>      <h1>你選擇的內容如下:</h1>      <p>名字:{{selectedSite.site}}</p>      <p>網址為:{{selectedSite.url}}</p>  </div></body></html>

運行結果:

用ng-repeat載入列表資料也可以但是有局限性,選擇的是一個字串:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>選擇框Select</title>    <script src="angular-1.4.1/angular.min.js"></script>    <script>        var app = angular.module(‘myApp‘, []);        app.controller(‘customersCtrl‘, function($scope) {            $scope.sites = [                {site:"Google",url:"http:www.google.com"},                {site:"百度",url:"http:www.baidu.com"},                {site:"搜狗",url:"http:www.sogou.com"}            ];        });    </script></head><body>  <div ng-app="myApp" ng-controller="customersCtrl">      <select ng-model="selectedSite">          <option ng-repeat="x in sites" value="{{x.url}}">{{x.site}}</option>      </select>      <h1>你選擇的內容是:{{selectedSite}}</h1>  </div></body></html>

  運行結果:

ng-options使用對象有很大的不同,使用對象作為資料來源, x 為鍵(key), y 為值(value),select控制項ng-options表示控制項的值是什麼,然後ng-model綁定了資料對應的資料來源:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>選擇框Select</title>    <script src="angular-1.4.1/angular.min.js"></script>    <script>        var app = angular.module(‘myApp‘, []);        app.controller(‘customersCtrl‘, function($scope) {            $scope.sites = {                site01 : "Google",                site02 : "Runoob",                site03 : "Taobao"            };        });    </script></head><body>  <div ng-app="myApp" ng-controller="customersCtrl">      <select ng-model="selectedSite" ng-options="x for (x, y) in sites">      </select>      你選擇的是:{{selectedSite}}  </div></body></html>

  運行結果 你選擇的值為在 key-value 對中的 value

 

此外value 在 key-value 對中也可以是個對象:選擇的值在 key-value 對的 value 中, 這是它是一個對象:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>選擇框Select</title>    <script src="angular-1.4.1/angular.min.js"></script>    <script>        var app = angular.module(‘myApp‘, []);        app.controller(‘customersCtrl‘, function($scope) {            $scope.cars = {                car01 : {brand : "Ford", model : "Mustang", color : "red"},                car02 : {brand : "Fiat", model : "500", color : "white"},                car03 : {brand : "Volvo", model : "XC90", color : "black"}            };        });    </script></head><body>  <div ng-app="myApp" ng-controller="customersCtrl">      <select ng-model="selectedCar" ng-options="y.brand for (x, y) in cars">      </select>      你選擇的是:{{selectedCar}}  </div></body></html>

  運行結果:

 

AngularJS(6)-選擇框Select

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.