Duplicates don't make ng-repeat errors when listing objects in an array

I am confused why ng-repeat gives me a duplication error. I can solve it with track by $index, but I want to know when this error is called angular.

It is clear

<div ng-repeat="a in [1,1,1,1]">...</div>

Since there is a duplicate value in the array above, it will certainly cause an error Dups.

How about a list of objects

<div ng-repeat="a in items">...</div>

Js

$scope.items = [
                   {"ab":1,"bc":3},
                   {"ab":1,"bc":3}
               ]

How does angular process / compare the second to determine if there are duplicate values ​​or not?

Thank.

EDIT

Why am I not getting duplication error?

Fiddle DEMO

+4
source share
3 answers

Finally, I clear the concepts myself.

Angular , . , , , , , . . .

function AController($scope) {
    var a = {"ab":1,"bc":3}
    var b = a;
    $scope.objsInObj = [
                   a,
                   b
               ]

}

DEMO

0

http://www.anujgakhar.com/2013/06/15/duplicates-in-a-repeater-are-not-allowed-in-angularjs/.

, (ab), . track by $index .

.

track by tracking_expression` -  *, DOM.  * , ng-repeat .  * . ( ,  * DOM, .) ,  * .

, ($$hashkey ), . . .

+4

angular Means (JSON, object hasOwnProperty), angular, , $$hashKey DOM, :

$scope.items = [
                   {"ab" : 1", "bc" : 3},
                   {"ab" : 1, "bc" : 3}
               ]

angular Simple Array

$scope.simpleArrray=[1, 1, 1, 1];

ng-repeat angularJS Hash Iterate JSON object Array, Duplicate ID, , , JSON object , JSON object

, .

function AController($scope) {
    var a = {"ab" : 1, "bc" :3}
    var b = a;
    $scope.objsInObj = [
                   a,
                   b
               ]    
}

In the above case, in ng-repeat, when the object ain Iteration angular adds $$hashKeyin a, when bin the iteration it points to awhich already has $$hashKey>, therefore angular will not add a new one $$hashKeyand will return a'sHashkey so that it is Duplicate, therefore angular Raup duplicate Error

+3
source

Source: https://habr.com/ru/post/1530979/


All Articles