Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-repeat not working when element repeat in object

This is my html

<div ng-app="person" ng-controller="names">
<div ng-repeat="id in ids">
{{id}}
</div>
</div>

This is my script

var persons=angular.module("person",[]);
persons.controller("names",function($scope){
$scope.ids=['vicky','a','c','svicky'];
});

This is working fine as expected working fiddle

But when i change the last element from 'svicky' to 'vicky' it is not working

fiddle which is not working

Why is this happening?

like image 299
Vignesh Subramanian Avatar asked Jun 10 '26 06:06

Vignesh Subramanian


1 Answers

Use track by $index as the console tells you. Fiddle

<div ng-app="person" ng-controller="names">
<div ng-repeat="id in ids track by $index">
{{id}}
</div>
</div>

This is the error you should see in the console with your non-working example:

Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by'

like image 197
camden_kid Avatar answered Jun 13 '26 14:06

camden_kid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!