具有相同名称的数组中的ng-repeat值

时间:2018-09-10 11:50:43

标签: arrays json angular angularjs-ng-repeat

在我的数组中,有多个具有相同名称的键值。我想在我的视图中列出所有这些内容,如何实现?

我遇到错误,提示[ngRepeat:dupes] Duplicates in a repeater are not allowed

这是我的JSON数组:-

["text-align", "space-before", "space-before.conditionality", "font-size", "font-weight", "line-height", "font-size", "font-weight", "line-height", "space-before", "font-size", "font-weight", "line-height", "position", "top", "bottom", "right", "left", "text-align", "force-page-count", "break-before", "font-size"]

我们可以看到很少有重复的键。我需要列出所有重复的值,但不要在视图中遗漏任何值

2 个答案:

答案 0 :(得分:1)

您使用ng-repeat会循环遍历数组中的唯一值。如果您有重复的值,则需要明确地提到Angular模块,以忽略这些值是唯一的,并通过以下操作查找唯一的index值:

ng-repeat = "item in items track by $index"

使用track by可以明确提到在使用ng-repeat渲染DOM时应检查哪个值作为唯一性度量。您甚至可以使用item in items track by item.id之类的对象的属性,其中item是具有id属性的对象,而items是对象数组。

您的数组在数组中多次指定了font-size,因此请使用track by $index

答案 1 :(得分:1)

您可以使用track by $index删除此错误,类似这样

<tag ng-repeat="item in items  track by $index">

//content

</tag>

https://docs.angularjs.org/api/ng/directive/ngRepeat

相关问题