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

ng-repeat value in array with same name

在我的数组中有多个具有相同名称的键值。我想在我的视图中列出它们,我该如何实现?

我遇到错误 [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"]

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

你用 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

您可以使用 track by $index 来消除这个错误,像这样

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

//content

</tag>

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