要循環的數據javascript
$scope.friends = [ {name:'John', age:25, gender:'boy'}, {name:'Jessie', age:30, gender:'girl'}, {name:'Johanna', age:28, gender:'girl'}, {name:'Joy', age:15, gender:'girl'}, {name:'Mary', age:28, gender:'girl'}, {name:'Peter', age:95, gender:'boy'}, {name:'Sebastian', age:50, gender:'boy'}, {name:'Erika', age:27, gender:'girl'}, {name:'Patrick', age:40, gender:'boy'}, {name:'Samantha', age:60, gender:'girl'} ];
ng-repeat 指令用於循環輸出指定次數的 HTML 元素。
集合必須是數組或對象。java
<element ng-repeat="expression"></element>
全部的 HTML 元素都支持該指令。react
使用數組循環輸出一個表格:angularjs
<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öp", "Country" : "Sweden" },{ "Name" : "Centro comercial Moctezuma", "Country" : "Mexico" },{ "Name" : "Ernst Handel", "Country" : "Austria" } ] }); </script>
使用對象循環輸出一個表格:express
<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>
item in items track by $id(item) 至關於 item in items
ng-repeat會爲每個元素加上一個$$hashKey來識別每個元素,當咱們從後端從新獲取數據時,即便數據徹底同樣,可是因爲hashKey不同,angular會刪除以前的全部dom,從新生成新的dom。這樣效率就會大大下降。能夠理解爲ng-repeat默認是 track by $hashKey的。因此,咱們應該使用一些不會變的東西來做爲標識,好比後端數據的id.後端
item in items track by item.id
這樣當從新獲取數據時,因爲id沒有變,angular就不會去刪除原來的dom,只會更新其中的內容,不一樣的id再添加新的dom。效率就能提高了。這至關於react中data-reactid的功能,這樣angular並不比react慢。api
item in items track by $index
按索引變量$index循環數組
ng-repeat和(ng-repeat-start,ng-repeat-end)都是循環一個items, 區別是ng-repeat-start,ng-repeat-end能夠跨越多個標籤, 而ng-repeat只能在一個標籤裏面循環!app
<header ng-repeat-start="friend in friends"> Header {{ friend.name }} </header> <div class="body"> Body {{ friend.age }} </div> <footer ng-repeat-end> Footer {{ friend.gender }} <hr> </footer>
ng-repeat裏面提供了幾個變量,爲開發者提供一些快捷的操做,dom
$index: 表示當前item的索引,
$first:若是item爲第一個,那麼$first爲true,
$middle:若是item不是開頭,不是結尾,那麼$middle爲true,
$last:若是item爲最後一個,那麼$last爲true,
$even:若是索引爲偶數,那麼$even爲true,
$odd:若是索引爲奇數,那麼$odd爲true
item in items | filter : x as results ,結果filter過濾後的 結果會被保存到results
I have {{friends.length}} friends. They are: <input type="search" ng-model="q" placeholder="filter friends..." /> <ul class="example-animate-container"> <li class="animate-repeat" ng-repeat="friend in friends | filter:q as results"> [{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old. </li> <li class="animate-repeat" ng-if="results.length === 0"> <strong>No results found...</strong> </li> </ul>
1,若是要循環的數組是這樣的$scope.dataList = [1,2,1];
會報錯
Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: user in users, Duplicate key: number:1。
這是由於ng-Repeat不容許collection中存在兩個相同Id的對象。
對於數字或者字符串等基本數據類型來講,它的id就是它自身的值。所以數組中是不容許存在兩個相同的數字的。爲了規避這個錯誤,須要定義本身的track by表達式。
若是是javascript對象類型數據,那麼就算內容一摸同樣,ng-repeat也不會認爲這是相同的對象。
若是$scope.dataList = [{"age":10},{"age":10}];,那麼是不會報錯的。
2,慎用$index做爲增刪改查數據的依據,因爲通過filter過濾後,index的值已經變化,會引發錯誤,最好直接取item對象去增刪改查!
參考自:http://blog.csdn.net/renfufei...
3,track by 必定要放在orderBy以後,不然會影響orderBy的效果。
參考文章:
https://docs.angularjs.org/ap...
http://www.tuicool.com/articl...