ng-repeat指令中使用track by子语句

来源:互联网 发布:新源代源码论坛 编辑:程序博客网 时间:2024/04/29 22:46

我们可以使用ng-repeat指令遍历一个javascript数组,当数组中有重复元素的时候,angularjs会报错:

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。下面的代码就会报错:

[html] view plain copy
 print?
  1. <html>  
  2.   <head>  
  3.     <script src="angular-1.2.2/angular.js"></script>  
  4.     <script>    
  5.          function rootController($scope,$rootScope,$injector)  
  6.          {  
  7.             $scope.dataList = [1,2,1];  
  8.          }  
  9.     </script>  
  10.   </head>  
  11.   <body ng-app ng-controller="rootController">  
  12.         <div ng-repeat="data in dataList">  
  13.             {{data}}      
  14.         </div>  
  15.   </body>  
  16. </html>  


这是因为ng-Repeat不允许collection中存在两个相同Id的对象。

For example: item in items is equivalent to item in items track by $id(item). This implies that the DOM elements will be associated by item identity in the array.

对于数字或者字符串等基本数据类型来说,它的id就是它自身的值。因此数组中是不允许存在两个相同的数字的。为了规避这个错误,需要定义自己的track by表达式。

// 业务上自己生成唯一的id
item in items track by item.id

//或者直接拿循环的索引变量$index来用
item in items track by $index


如果是javascript对象类型数据,那么就算内容一摸一样,ng-repeat也不会认为这是相同的对象。如果将上面的代码中dataList,那么是不会报错的。比如$scope.dataList = [{"age":10},{"age":10}];


原网址:http://blog.csdn.net/aitangyong/article/details/44100921

阅读全文
0 0
原创粉丝点击