如何在角度中使用* ngFor遍历“对象携带数组”?

时间:2019-04-02 17:14:37

标签: angular angular6-json-schema-form

我正在尝试遍历包含数组的对象,但收到错误消息“找不到类型为'object'的其他支持对象'[object Object]'。NgFor仅支持绑定到Iterables,例如Arrays。”以下是我的json响应:

data={"books": [{"title": "A","edition": 3,"year": 2011 },
{"title": "B","edition": 2,"year": 2009},
{"title": "C","edition": 2,"year": 2008}],
"count":3
}

模板代码:

<tr *ngFor="let d of data">
<td>d.books.title</td>
</tr>

任何人都可以建议需要进行哪些更改才能遍历数据吗?

2 个答案:

答案 0 :(得分:1)

您需要使用数组,即data.books

<tr *ngFor="let d of data.books">
<td>{{d.title}}</td> //edited after looking at comments
</tr>

答案 1 :(得分:0)

In your .ts file is : Declare the data object
  data =
      {"books": 
      [
        {"title": "A","edition": 3,"year": 2011 },
        {"title": "B","edition": 2,"year": 2009},
        {"title": "C","edition": 2,"year": 2008}
      ],
        "count":3
      }

In your .html file:

<table>
    <tr *ngFor="let d of data.books">
     <td>{{d.title}}</td>
    </tr>
</table>

我尝试了。。它对我有用! enter image description here