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