AngularJS基礎 ng-repeat 指令簡單樣本_AngularJS

來源:互聯網
上載者:User

AngularJS ng-repeat 指令

AngularJS 執行個體

迴圈輸出多個標題:

<!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 ng-app="myApp" ng-controller="myCtrl"><h1 ng-repeat="x in records">{{x}}</h1><script>var app = angular.module("myApp", []);app.controller("myCtrl", function($scope) { $scope.records = [ "菜鳥教程1", "菜鳥教程2", "菜鳥教程3", "菜鳥教程4", ]});</script></body></html>

定義和用法

ng-repeat 指令用於迴圈輸出指定次數的 HTML 元素。

集合必須是數組或對象。

文法

<element ng-repeat="expression"></element>

所有的 HTML 元素都支援該指令。

參數值

描述
expression 運算式定義了如何迴圈集合。

運算式執行個體規則:

x in records

(key, value) in myObj

x in records track by $id(x)

更多執行個體

AngularJS 執行個體

使用數組迴圈輸出一個表格:

<!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 ng-app="myApp"><table ng-controller="myCtrl" border="1"><tr ng-repeat="x in records"> <td>{{x.Name}}</td> <td>{{x.Country}}</td> </tr></table><script>var app = angular.module("myApp", []);app.controller("myCtrl", function($scope) { $scope.records = [ {  "Name" : "Alfreds Futterkiste",  "Country" : "Germany" }, {  "Name" : "Berglunds snabbk",  "Country" : "Sweden" }, {  "Name" : "Centro comercial Moctezuma",  "Country" : "Mexico" }, {  "Name" : "Ernst Handel",  "Country" : "Austria" } ]});</script></body></html>

運行結果:

Alfreds Futterkiste Germany
Berglunds snabbk Sweden
Centro comercial Moctezuma Mexico
Ernst Handel Austria

AngularJS 執行個體

使用對象迴圈輸出一個表格:

<!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 ng-app="myApp"><table ng-controller="myCtrl" border="1"><tr ng-repeat="(x, y) in myObj"> <td>{{x}}</td> <td>{{y}}</td> </tr></table><script>var app = angular.module("myApp", []);app.controller("myCtrl", function($scope) { $scope.myObj = { "Name" : "Alfreds Futterkiste", "Country" : "Germany", "City" : "Berlin" }});</script></body></html>

運行結果:

Name Alfreds Futterkiste
Country Germany
City Berlin

以上就是對AngularJS ng-repeat 指令的基礎資料整理,後續繼續補充。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.