2016-09-15 10 views
5

Angular2 rc.6angular2 NgFor obsługuje tylko wiązanie się Iterables takich jak macierze

otrzymuję następujący błąd podczas wykonywania pętli na danych JSON

core.umd.js: 5995 WYJĄTEK: Błąd w aplikacji/modules/mbs/components/menu.html: 5: 4 spowodowane przez: Nie można znaleźć obiektu pomocniczego "[object Object]" typu "object". NgFor obsługuje tylko powiązanie z Iterables, takie jak Arrays.

Moja pętla html, Uwaga: Po prostu chcę powtórzyć właściwości i tablice wewnątrz odpowiedzi.

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

moja metoda usługa

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

} 

Oto moja odpowiedź 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

A co jest wynikiem można się spodziewać? – dfsq

+1

Co to jest, co chcesz wyświetlić? Jak pokazuje błąd, nie można po prostu użyć NgFor do zapętlenia elementów w obiekcie. Są sposoby, aby to naprawić, ale muszę zrozumieć, co chcesz zrobić najpierw –

+0

dzięki za komentarz. Po prostu chcę wyświetlać właściwości i iterować przez tablicę potomną –

Odpowiedz

10

Wygląda jak chcesz

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