0

angular.module("myApp",[])
       .controller("myCtrl",function($scope) {
         $scope.persons = [{name:"teja",age:11},
                           {name:"Ash",age:12},
                           {name:"teja",age:11}];
                           });

Angular doesn't allow duplicate elements in an array declared in ng-repeat. But there is solution "track by $index" which can allow duplicate elements in array. Here my question is how ng-repeat identify there are duplicate elements in the array that is on what factor it identifies. Whenever a new object is created new reference for that object is created but in the above code how ng-repeat identifies the duplicates.

<div ng-repeat="person in  persons">
{{ person.name }}
</div>
Tejaswi katha
  • 29
  • 1
  • 17

1 Answers1

0

From the documentation

To minimize creation of DOM elements, ngRepeat uses a function to "keep track" of all items in the collection and their corresponding DOM elements. For example, if an item is added to the collection, ngRepeat will know that all other items already have DOM elements, and will not re-render them.

The default tracking function (which tracks items by their identity) does not allow duplicate items in arrays. This is because when there are duplicates, it is not possible to maintain a one-to-one mapping between collection items and DOM elements.

So if you have two elements that are exactly the same (so if angular.equals(a, b)) angular will not be able to keep track of the relationship between DOM elements and items and will send an error

Community
  • 1
  • 1
Matteo Cardellini
  • 876
  • 2
  • 17
  • 41