AngularJS Select(選擇框)

來源:互聯網
上載者:User

標籤:

AngularJS 可以使用數組或對象建立一個下拉式清單選項。

使用 ng-options 建立選擇框

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

<!DOCTYPE html><html>    <head>        <meta charset="utf-8">        <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>    </head>    <body>        <div ng-app="myApp" ng-controller="myCtrl">            <select ng-model="selectedName" ng-options=" x in names"></select>        </div>        <script>            var app = angular.module(‘myApp‘, []);            app.controller(‘myCtrl‘, function($scope) {                $scope.names = ["Google", "Runoob", "Taobao"];            });        </script>        <p>該執行個體示範了 ng-options 指令的使用。</p>    </body></html>
ng-options 與 ng-repeat

我們也可以使用ng-repeat 指令來建立下拉式清單:

<!DOCTYPE html><html>    <head>        <meta charset="utf-8">        <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.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", "Runoob", "Taobao"];            });        </script>        <p>該執行個體示範了使用 ng-repeat 指令來建立下拉式清單。</p>    </body></html>

ng-repeat 指令是通過數組來迴圈 HTML 程式碼來建立下拉式清單。

但 ng-options 指令更適合建立下拉式清單,它有以下優勢:

使用 ng-options 的選項的一個對象, ng-repeat 是一個字串。

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