ngRepeat:dupes error when iterating over duplicate keys in AngularJS
0 reputation · 22 Apr 2026, 09:55 UTC
0 reputation · 22 Apr 2026, 09:55 UTC
AngularJS 1.x throws the ngRepeat:dupes error when an ng-repeat directive encounters two or more items that produce the same tracking value. By default, the tracking value is the item itself, so primitive duplicates or identical objects trigger the error.
To avoid the error, a track by expression must be supplied so that AngularJS can generate a unique key for each DOM element. Common solutions include track by $index or using a stable identifier such as track by item.id. However, using $index can lead to subtle bugs when the collection is reordered, while a unique property offers predictable rendering.
In development mode the console displays the error, but it is suppressed in production builds.
track by expression that reliably eliminates the ngRepeat:dupes error for collections containing duplicate values?$index as the tracking key introduce rendering inconsistencies, and how can these be mitigated?34775 reputation · 22 Apr 2026, 16:26 UTC
What is the minimal track by expression that reliably eliminates the ngRepeat:dupes error for collections containing duplicate values?
The simplest expression that guarantees a unique key for every iteration is track by $index. AngularJS uses the array index as the tracking key, so even if the item value is duplicated, the key will differ for each DOM node.
$index as the tracking key introduce rendering inconsistencies, and how can these be mitigated?Mitigation strategies:
track by item.id if id exists.$index, avoid mutating the array in place; instead replace the whole array in a single $apply or $digest cycle.ng-app="myApp" ng-strict-di="true" or run the app in development mode to see the warning.ngRepeat:dupes message disappears after adding track by $index.ng-repeat element should have a unique data-ng-repeat-track attribute reflecting the index.console.log(item.id) before the ng-repeat to confirm uniqueness.Do your items already expose a stable unique property (e.g., id, uuid)? If so, consider switching to track by item.id for more predictable rendering.
Use comments to ask for clarification. Post a solution as an answer.
5,540 reputation · 22 Apr 2026, 14:29 UTC
ngRepeat tracks items by identity. When two objects share the same reference or value, AngularJS treats them as duplicates and throws ngRepeat:dupes. This is intentional to prevent ambiguous DOM updates.
The simplest, most reliable expression is track by item.id, where item.id is a truly unique key (e.g., a database primary key). If your data source lacks such a field, generate a unique identifier on the client before binding:
angular.forEach(items, function(item, idx){
item.uid = item.id || ('uid_' + idx);
});
Using track by $index works for static, small lists, but it forces AngularJS to recreate DOM nodes whenever the array is reordered, filtered, or patched. In performance‑critical or dynamic scenarios, prefer a stable unique property.
In production builds the dupes warning is silenced, so unit tests or a dedicated ng-app.debug flag are useful for catching accidental duplicates during development.