AngularJS基礎(chǔ) ng-repeat 指令簡(jiǎn)單示例
AngularJS ng-repeat 指令
AngularJS 實(shí)例
循環(huán)輸出多個(gè)標(biāo)題:
<!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 指令用于循環(huán)輸出指定次數(shù)的 HTML 元素。
集合必須是數(shù)組或?qū)ο蟆?br />
語法
<element ng-repeat="expression"></element>
所有的 HTML 元素都支持該指令。
參數(shù)值
值 | 描述 |
---|---|
expression | 表達(dá)式定義了如何循環(huán)集合。 表達(dá)式實(shí)例規(guī)則: x in records (key, value) in myObj x in records track by $id(x) |
更多實(shí)例
AngularJS 實(shí)例
使用數(shù)組循環(huán)輸出一個(gè)表格:
<!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>
運(yùn)行結(jié)果:
Alfreds Futterkiste | Germany |
Berglunds snabbk | Sweden |
Centro comercial Moctezuma | Mexico |
Ernst Handel | Austria |
AngularJS 實(shí)例
使用對(duì)象循環(huán)輸出一個(gè)表格:
<!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>
運(yùn)行結(jié)果:
Name | Alfreds Futterkiste |
Country | Germany |
City | Berlin |
以上就是對(duì)AngularJS ng-repeat 指令的基礎(chǔ)資料整理,后續(xù)繼續(xù)補(bǔ)充。
相關(guān)文章
利用angularjs1.4制作的簡(jiǎn)易滑動(dòng)門效果
本文主要介紹了利用angularjs1.4制作的簡(jiǎn)易滑動(dòng)門效果的實(shí)例,具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02AngularJS遞歸指令實(shí)現(xiàn)Tree View效果示例
這篇文章主要介紹了AngularJS遞歸指令實(shí)現(xiàn)Tree View效果,結(jié)合實(shí)例形式分析了AngularJS基于遞歸指令實(shí)現(xiàn)樹形結(jié)構(gòu)層次數(shù)據(jù)的相關(guān)操作步驟與注意事項(xiàng),需要的朋友可以參考下2016-11-11AngularJS中的Directive實(shí)現(xiàn)延遲加載
延遲加載通常是直到用戶交互時(shí)才加載,那么如何實(shí)現(xiàn)延時(shí)加載的呢?下面通過本文一起學(xué)習(xí)AngularJS中的Directive實(shí)現(xiàn)延遲加載,對(duì)angularjs延時(shí)加載相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01AngularJS實(shí)現(xiàn)自定義指令與控制器數(shù)據(jù)交互的方法示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)自定義指令與控制器數(shù)據(jù)交互的方法,結(jié)合具體實(shí)例形式分析了AngularJS自定義指令的實(shí)現(xiàn)以及與控制器數(shù)據(jù)交互的操作技巧,需要的朋友可以參考下2017-06-06Angular 多級(jí)路由實(shí)現(xiàn)登錄頁面跳轉(zhuǎn)(小白教程)
這篇文章主要介紹了Angular 多級(jí)路由實(shí)現(xiàn)登錄頁面跳轉(zhuǎn)(小白教程),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11高效利用Angular中內(nèi)置服務(wù)$http、$location等
這篇文章主要介紹了如何高效利用Angular中內(nèi)置服務(wù),大家知道的Angular內(nèi)置服務(wù)有哪些,感興趣的小伙伴們可以參考一下2016-03-03