Angular2 rc.6
在json数据上运行循环时出现以下错误
core.umd.js:5995 EXCEPTION:app / modules / mbs / components / menu.html:5:4引起的错误:无法找到'object'类型的不同支持对象'[object Object]'。 NgFor仅支持绑定到Iterables,例如Arrays。
我的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" } ] } ] }
答案 0 :(得分:15)
看起来你想要
<li *ngFor="let item of menuItems.children">
{{item}}
</li>
答案 1 :(得分:1)
通常来说,该错误表示您正在尝试迭代对象,而不是数组或可观察对象。
答案 2 :(得分:1)
如果您要遍历对象:
<div *ngFor="let location of locations | keyvalue">
{{location.key}} - {{location.value | json}}
</div>