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

本文介绍了如何解决AngularJS中ngRepeat指令产生的Error:[ngRepeat:dupes]错误,该错误提示不允许在循环中有重复项。文章提供了两种解决方法:一是为每个元素生成唯一ID,二是使用索引作为唯一标识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

报错: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>

转载于:https://www.cnblogs.com/xiaodangshan/p/7346111.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值