I have seen few questions in SO discussing no duplicates allowed in ng-repeat. My question is little different. In my case I am confused because I am not getting the error even if there are duplicate objects in array
Here is my HTML code
<table>
<tr ng-repeat="item in items">
<td> {{ item.email}}</td>
</tr>
</table>
And here is the code for populating the array
app.controller('MainCtrl', function($scope) {
$scope.items=[];
$scope.items.push({
"id":"1",
"email":"[email protected]"});
$scope.items.push({
"id":"1",
"email":"[email protected]"});
$scope.items.push({
"id":"2",
"email":"[email protected]"});
$scope.items.push({
"id":"2",
"email":"[email protected]"});
});
As per my understanding I should get the error and there are duplicate objects in array
However its getting rendered perfectly. Here is the plunker link
Obviously I am missing something basic. Can somebody point out my gap in understanding?
EDIT
Here is what in my application I was facing (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 [“[email protected]", “[email protected]", “[email protected]", “[email protected]"]
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:[email protected], Duplicate value: “[email protected]"
I updated the plunker with similar code. Not able to reproduce
Second Edit
The problem was with the version I was using: Angular JS 1.0.x supported duplicates not able to reproduce http://plnkr.co/edit/qrOez7aZ7X1jsOrmkfiP?p=preview
With the later version able to reproduce
{id: 1}may look like{id: 1}, but ``{id: 1} === {id: 1}` will returnfalse, because it's creating a new Object for each. This is because Objects, and Arrays, are tracked by reference, not by value. Now, if you didvar objX = { "id":"1", "email":"[email protected]"}, and then$scope.items.push(objX); $scope.items.push(objX);you'll get the error (becauseobjX === objXistrue)