Angular_Newbie
Angular_Newbie

Reputation: 417

How do I prevent duplicate Angular keys in ng-repeat?

Having trouble with duplicate keys in NG-Grid. Search for Tim then click on ng grid column in gridOptions1 to see duplicates in gridOptions2. Any ideas?

Here's plnkr

$scope.gridColumnDefs2 = [
 {displayName:'Phone', cellTemplate: 
 '<div data-ng-repeat="(key, ngClickResult) in ngClickResults track by $index">{{ngClickResult.phone}}</div>'},  

Upvotes: 0

Views: 918

Answers (2)

ajmajmajma
ajmajmajma

Reputation: 14216

I like to use track by to solve this, unless there is some reason you should not, try track by $index - that usually does the trick!

<div data-ng-repeat="(key, ngClickResult) in ngClickResults track by $index">

Or if you're actually trying to remove the items from the data you can filter , something like this from this answer here - AngularJs Remove duplicate elements in ng-repeat

Although If you use this second method, unless you are really stuck I would recommend trying to modify the data coming in before it hits the repeat - maybe on the server if you have access to that.

Upvotes: 2

Alexandru Olaru
Alexandru Olaru

Reputation: 7092

Angular provide the unique filter:

<div ng-repeat="item in result | unique:'_id'">
    //Body here
</div>

Upvotes: 0

Related Questions