【发布时间】:2019-04-02 03:46:27
【问题描述】:
我想遍历对象中的键值对并每行显示两个项目。我查看了类似 Example 的其他示例,但我不知道如何向其中添加键值。
这是我的代码:
<div *ngFor="let item of book.bookData.privateData | keyvalue; index as i; let even = even">
<div fxFlex="100" fxLayout="row" *ngIf="even">
<div fxFlex="50" fxLayout="column">
Key: <b>{{book.bookData.privateData[i].key}}</b> and Value: <b>{{book.bookData.privateData[i].value}}</b>
</div>
<div fxFlex="50" fxLayout="column">
Key: <b>{{book.bookData.privateData[i+1].key}}</b> and Value: <b>{{book.bookData.privateData[i+1].value}}</b>
</div>
</div>
</div>
这不起作用,因为privateData 对象上没有key 和value 属性,这些属性被分配给item。
提前感谢您的帮助!
编辑:
以下是我正在努力实现的工作示例,但这显然不是一种有效的方法:
<div *ngFor="let item of bookState.bookData.privateData | keyvalue; index as i; let even = even">
<div fxFlex="100" fxLayout="row" *ngIf="even">
<div fxFlex="50">
<div *ngFor="let deeperItem1 of bookState.bookData.privateData | keyvalue; index as i2">
<div *ngIf="i2 === i">
<b>INDEX {{i2}} and {{i}} </b>Key: <b>{{deeperItem1.key}}</b> and Value: <b>{{deeperItem1.value}}</b>
</div>
</div>
</div>
<div fxFlex="50">
<div *ngFor="let deeperItem2 of bookState.bookData.privateData | keyvalue; index as i3">
<div *ngIf="(i3-1) === i">
<b>INDEX {{i3}} and {{i}} </b>Key: <b>{{deeperItem2.key}}</b> and Value: <b>{{deeperItem2.value}}</b>
</div>
</div>
</div>
</div>
</div>
EDIT2:
要指定问题:问题不在于键值管道不起作用,问题在于如何有效地向这些管道添加索引以显示每行项目的 x 数量(在我的情况下为 2)。 造成误会请见谅!
【问题讨论】:
-
你能添加一个你正在循环的模式吗?项目中有什么?
-
keyvalue管道到底在做什么或试图做什么?可以分享pirvateData的界面吗? -
privateData 包含任意数量的键值对作为 JSON 响应。 key 和 value 都是字符串。