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

后端 未结 10 2582
青春惊慌失措
青春惊慌失措 2020-11-22 06:11

I am defining a custom filter like so:

10条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 06:37

    I was having an issue in my project where I was using ng-repeat track by $index but the products were not getting reflecting when data comes from database. My code is as below:

    In the above code, product is a separate directive to display the product.But i came to know that $index causes issue when we pass data out from the scope. So the data losses and DOM can not be updated.

    I found the solution by using product.id as a key in ng-repeat like below:

    But the above code again fails and throws the below error when more than one product comes with same id:

    angular.js:11706 Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater

    So finally i solved the problem by making dynamic unique key of ng-repeat like below:

    This solved my problem and hope this will help you in future.

提交回复
热议问题