0

我正在尝试遍历包含数组的对象,但出现错误“找不到类型为 'object' 的不同支持对象 '[object Object]'。NgFor 仅支持绑定到 Iterables,例如数组。” 以下是我的 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>

谁能建议需要进行哪些更改才能遍历数据?

4

2 回答 2

1

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

<tr *ngFor="let d of data.books">
<td>{{d.title}}</td> //edited after looking at comments
</tr>
于 2019-04-02T17:17:05.990 回答
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>

我试过了..它对我有用! 在此处输入图像描述

于 2019-04-02T17:38:06.107 回答