AngularJS Ng-repeat and duplicates

I saw several questions in SO that did not discuss any duplicates allowed in ng-repeat. My question is a little different. In my case, I am confused because I am not getting an error, even if there are duplicate objects in the array

Here is my HTML code

<table> <tr ng-repeat="item in items"> <td> {{ item.email}}</td> </tr> </table> 

And here is the code to populate the array

 app.controller('MainCtrl', function($scope) { $scope.items=[]; $scope.items.push({ "id":"1", "email":" a@b.com "}); $scope.items.push({ "id":"1", "email":" a@b.com "}); $scope.items.push({ "id":"2", "email":" x@y.com "}); $scope.items.push({ "id":"2", "email":" x@y.com "}); }); 

According to my understanding, I should get an error and there are duplicate objects in the array

However, its receipt is excellent. Here is the plunker link

ng-repeat-demo

Obviously, I am missing something basic. Can anyone point out my gap in understanding?

EDIT

This is what I came across in my application (only email IDs are changed for obvious reason)

ExampleApp.filter ("extractEmail", function () {

  return function(items){ //console.log("test" + input[0].highlight.file[0]); var filtered = []; console.log(" items == " + items); angular.forEach(items, function(item){ if (item){ var obj = item.highlight.file[0].toString().match(/([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})/); if (obj) { //console.log(obj[0]); filtered.push(obj[0]); } } }); console.log(filtered); return filtered; } }); 

my console.log prints [" a@gmail.com ", " a@gmail.com ", " b@gmail.com ", " b@gmail.com "]

the error i get

 Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: x in clusterState| extractEmail, Duplicate key: string: a@b.com , Duplicate value: " a@b.com " 

I updated the plunker using similar code. Unable to play

Second edit

The problem was in the version I used: Angular Supported Duplicates JS 1.0.x cannot play http://plnkr.co/edit/qrOez7aZ7X1jsOrmkfiP?p=preview

With a later version capable of reproducing

http://plnkr.co/edit/q3oPCrW3RepxvWSZ1LB1?p=preview

+6
source share
2 answers

Objects in javascript are compared by reference, not by value.
Actually, it doesn’t matter if the contents of the object coincide with the other, if the links do not point to the same object, they are different.
For instance:.

 // In this case "var a" and "var b" points to different objects. var a = {}; var b = {}; a == b; // false // Here, they points to the same one var a = {}; var b = a; a == b; // true 

If you want each entry to be separate, you must verify each entry yourself.
Angular ngRepeat has a syntax change that uses track by so you can determine which record is different (or duplicated).

 <div ng-repeat="entry for entries track by entry.id">{{entry.email}}</div> 
+5
source

Elements are not considered equal because they point to different links.

If you must track by ID, then you will get an error because the identifiers are equal.

 <tr ng-repeat="item in items track by item.id"> <td> {{ item.email}}</td> </tr> 

See this script: http://jsfiddle.net/aj_r/7d4n9z0u/

+5
source

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


All Articles