0

我需要根据情况重复一个集合或一个数组。

如果以下方法导致“ngRepeat:dupes”,我该怎么做,代码片段如下:

<div ng-if="cause.length" ng-repeat="k in cause">
    {{k}}
</div>
<div ng-if="!cause.length" ng-repeat="(k,v) in cause">
    {{k}}
</div>
4

1 回答 1

0

add track by $index to end of ng-repeat declaration any you will not get any Dupes error...

<div ng-if="cause.length" ng-repeat="k in cause track by $index">
    {{k}}
</div>
<div ng-if="!cause.length" ng-repeat="(k,v) in cause track by $index">
    {{k}}
</div>

UPDATE

If no tracking function is specified the ng-repeat associates elements by identity in the collection. It is an error to have more than one tracking function to resolve to the same key. (This would mean that two distinct objects are mapped to the same DOM element, which is not possible.)

and this explanation from angularjs documentation explains why it resolve this error...

于 2014-06-10T10:02:24.117 回答