2017-09-26 34 views
0

有沒有辦法動態創建itemsAndPageableInfo對象,然後例如在視圖itemsAndPageableInfo.content打印?get()從服務器動態創建對象

itemsAndPageableInfo : {};  

    this.itemService.getItems(0, 2) 
       .subscribe(res => { 
       this.itemsAndPageableInfo = res.json(); 
       this.items = res.json().content; 
       }); 

我試圖做<tr *ngFor="let item of itemsAndPageableInfo.content; let i = index">但我得到的是ERROR TypeError: Cannot read property 'content' of undefined

+2

使用安全運算符:'' –

回答

1

需要初始化itemsAndPageableInfo到一個空迭代或不顯示<tr>直到它加載了一個錯誤。

原因是itemsAndPageableInfo被異步填充,並且數據顯示在DOM中加載元素後導致空引用。