angular2 NgFor仅支持绑定到Iterables如阵列

问题描述 投票:8回答:3

Angular2 rc.6

上运行JSON数据循环时收到以下错误

core.umd.js:5995例外:错误的应用程序/模块/ MBS /组件/ menu.html:5:4所造成的:无法找到一个不同支承对象类型“对象”的“[对象的对象]”。 NgFor只支持绑定到Iterables比如数组。

我的html循环,注意:我只是想重复的响应特性内和数组。

<li *ngFor="let item of menuItems">
{{item}}
</li>

我的服务方法

getMenuItems():Promise<any>{
    return this.http.get('api/menu').toPromise().
    then(response => response.json())
        .catch(this.handleError)

}

以下是我的JSON响应

{ "text": "Menu", "children": [ { "text": "Home", "url": "/spatt-web/home" }, { "text": "Configure", "children": [ { "text": "Tri-Party Program", "children": [ { "text": "Margins and Filters", "url": "/sp-rrp/config/operation" }, { "text": "Fields and Desirability", "url": "/spatt-rrp/config/program" } ] }, { "text": "Shared Settings", "url": "/shared-config/config" }, { "text": "SOMA Limits", "url": "/outright-config/config" } ] }, { "text": "Plan", "children": [ { "text": "Tri-Party RRP Operations", "url": "/spatt-rrp/plan" } ] }, { "text": "Track" }, { "text": "Administer" }, { "text": "Help", "children": [ { "text": "RRP Operations", "url": "RRPference" }, { "text": "RRP Margin Calls and Recalls", "url": "RRPRecallference" } ] } ] }
angular angular2-services
3个回答
18
投票

它看起来像你想

<li *ngFor="let item of menuItems.children">
{{item}}
</li>

2
投票

一般来说,错误意味着您正试图重复的对象,而不是数组或可观察的。


1
投票

如果你试图重复的对象上:

  <div *ngFor="let location of locations | keyvalue">
       {{location.key}} - {{location.value | json}}
  </div>

KeyValuePipe Documntation

© www.soinside.com 2019 - 2024. All rights reserved.