angular 2-如何迭代JSON响应,该响应包含一个包含对象数组的对象

angular 2 - how do I iterate through a JSON response that contains an object which contains an array of objects?

本文关键字:响应 对象 包含一 包含 数组 JSON 何迭代 迭代 angular      更新时间:2023-09-26

我使用的是我朋友创建的一个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>