Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ng-repeat Error "Duplicates in a repeater are not allowed."

I am defining a custom filter like so:

<div class="idea item" ng-repeat="item in items" isoatom>         <div class="section comment clearfix" ng-repeat="comment in item.comments | range:1:2">         ....     </div> </div> 

As you can see the ng-repeat where the filter is being used is nested within another ng-repeat

The filter is defined like this:

myapp.filter('range', function() {     return function(input, min, max) {         min = parseInt(min); //Make string input int         max = parseInt(max);         for (var i=min; i<max; i++)             input.push(i);         return input;     }; }); 

I'm getting:

Error: Duplicates in a repeater are not allowed. Repeater: comment in item.comments | range:1:2 ngRepeatAction@https://ajax.googleapis.com/ajax/libs/angularjs/1.1.4/an

like image 241
Dine Avatar asked Apr 30 '13 09:04

Dine


People also ask

How do you prevent duplicates in NG-repeat?

You can use unique filter while using ng-repeat . If you use track by $index then unique won't work.

Why we use NG-repeat in Angular?

Example# ng-repeat is a built in directive in Angular which lets you iterate an array or an object and gives you the ability to repeat an element once for each item in the collection. To repeat multiple DOM elements by defining a start and an end point you can use the ng-repeat-start and ng-repeat-end directives.

What does ng-repeat do?

Definition and Usage. The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object.

Where is the last element in NG-repeat?

You can use $last variable within ng-repeat directive. Take a look at doc. Where computeCssClass is function of controller which takes sole argument and returns 'last' or null .


2 Answers

The solution is actually described here: http://www.anujgakhar.com/2013/06/15/duplicates-in-a-repeater-are-not-allowed-in-angularjs/

AngularJS does not allow duplicates in a ng-repeat directive. This means if you are trying to do the following, you will get an error.

// This code throws the error "Duplicates in a repeater are not allowed. // Repeater: row in [1,1,1] key: number:1" <div ng-repeat="row in [1,1,1]"> 

However, changing the above code slightly to define an index to determine uniqueness as below will get it working again.

// This will work <div ng-repeat="row in [1,1,1] track by $index"> 

Official docs are here: https://docs.angularjs.org/error/ngRepeat/dupes

like image 78
Webnet Avatar answered Oct 26 '22 02:10

Webnet


For those who expect JSON and still getting the same error, make sure that you parse your data:

$scope.customers = JSON.parse(data) 
like image 21
usefulBee Avatar answered Oct 26 '22 01:10

usefulBee