Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 0604bb7

Browse files
smillibtford
authored andcommitted
fix(ngRepeat): improve errors for duplicate items
-Log the value that had the duplicate key, as well as the key The error that is thrown when items have duplicate track by keys can be confusing because only the duplicate key is logged. If the user didn't provide that key themselves, they may not know what it is or what item it corresponds to.
1 parent 9257674 commit 0604bb7

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/ng/directive/ngRepeat.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,9 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
364364
forEach(nextBlockOrder, function (block) {
365365
if (block && block.scope) lastBlockMap[block.id] = block;
366366
});
367-
throw ngRepeatMinErr('dupes', "Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: {0}, Duplicate key: {1}",
368-
expression, trackById);
367+
throw ngRepeatMinErr('dupes',
368+
"Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: {0}, Duplicate key: {1}, Duplicate value: {2}",
369+
expression, trackById, toJson(value));
369370
} else {
370371
// new never before seen block
371372
nextBlockOrder[index] = {id: trackById, scope: undefined, clone: undefined};

test/ng/directive/ngRepeatSpec.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,8 @@ describe('ngRepeat', function() {
10961096
scope.items = [a, a, a];
10971097
scope.$digest();
10981098
expect($exceptionHandler.errors.shift().message).
1099-
toMatch(/^\[ngRepeat:dupes\] Duplicates in a repeater are not allowed\. Use 'track by' expression to specify unique keys\. Repeater: item in items, Duplicate key: object:3/);
1099+
toMatch(
1100+
/^\[ngRepeat:dupes\] Duplicates in a repeater are not allowed\. Use 'track by' expression to specify unique keys\. Repeater: item in items, Duplicate key: object:3, Duplicate value: {}/);
11001101

11011102
// recover
11021103
scope.items = [a];
@@ -1116,7 +1117,8 @@ describe('ngRepeat', function() {
11161117
scope.items = [d, d, d];
11171118
scope.$digest();
11181119
expect($exceptionHandler.errors.shift().message).
1119-
toMatch(/^\[ngRepeat:dupes\] Duplicates in a repeater are not allowed\. Use 'track by' expression to specify unique keys\. Repeater: item in items, Duplicate key: object:9/);
1120+
toMatch(
1121+
/^\[ngRepeat:dupes\] Duplicates in a repeater are not allowed\. Use 'track by' expression to specify unique keys\. Repeater: item in items, Duplicate key: object:9, Duplicate value: {}/);
11201122

11211123
// recover
11221124
scope.items = [a];

0 commit comments

Comments
 (0)