When using angularjs procedure, track by $index of NG repeat

When developing web applications, we often need to use ng repeat to traverse the array in $scope to update the list elements in the view
when the elements in our array are repeated, the browser will report such an error in the console

$scope.array=[1, 1, 1, 1, 2];
Error: [ngR epeat:dupes ]

Because angularjs forbids the repetition of elements in the array in ng repeat, we usually use track by $index to solve this problem

$scope.number=[1, 1, 1, 1, 2];

ng-repeat=”n in number track by $index”

The duplicate values here refer to the original values. If we use objects to represent them, there will be no errors. For example, the number object:

$scope.number=[new Number(2), new Number(2), new Number(2)];

String object:

$scope.number=[new String(2), new String(2), new String(2)];

Similar Posts: