angularjs使用ng-repeat报错Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed

报错:Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys.

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

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

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

原代码:<div ng-repeat="edu in educations ">

修改后:<div ng-repeat="edu in educations track by $index">

原代码:

<div ng-init="months = ['八月','八月','八月']" ng-repeat="month in months">
  {{month}}
</div
修改后:
<div ng-init="months = ['八月','八月','八月']" ng-repeat="month in months track by $index">
  {{month}}
</div>
posted @ 2017-08-11 15:18  xiaodangshan  阅读(469)  评论(0编辑  收藏  举报