我正在使用我 friend 创建的 Web API,以下是示例响应:
{
types: [
{
id: 1,
contents: [
{
id: 1001,
perishable: 0
},
{
id: 1002,
perishable: 0
}
]
}
]
}
因此,如果我收到的响应包含 3 种类型的内容,并且每种类型都有 3 个内容,那么在下面的场景中如何使用转发器?我能够正确获取类型 ID,但在重复内容时遇到问题。
<div *ngFor="let type of response.types">
<h2>{{type.id}}</h2>
<!-- here I want to show basically divs for each of the objects in contents -->
<div *ngFor="what should I do here?">
<p>{{content.id}}</p>
<p>{{content.perishable}}</p>
</div>
</div>
最佳答案
<div *ngFor="let type of response.types">
<h2>{{type.id}}</h2>
<div *ngFor="let content of type.contents">
<p>{{content.id}}</p>
<p>{{content.perishable}}</p>
</div>
</div>
关于javascript - Angular 2 - 如何迭代包含对象数组的 JSON 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38681583/