2017-08-13 203 views
0

我有一個多維數組與兩個鍵,它看起來像這樣。Angular 4 |儘管多維陣列循環

user: any = {}; 
// index is from a for loop to add to the user (this part works perfectly) 
this.user[index++] = { 
    Name: name, 
    Age: age, 
} 

但是我的問題是,我想使用* ngFor通過循環在我的角度HTML模板,但是我收到一個錯誤說它不支持它。所以我的問題是,我如何循環訪問這個用戶數組並打印出數組中每個鍵的名稱和年齡值?

我嘗試:

<li *ngFor="let user of users">{{user.Name}} - {{user.Age}}</li> 

的錯誤我收到

ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. 

謝謝。

+0

'user:any = {};'不是數組!嘗試'user:any = [];' –

+1

^他不是試圖訪問對象鍵值,只是聲明是錯誤的。 'user:any [] = []'應該修正它? – Skeptor

+0

@MohamedAbbas唉,你說得對。我一生中有一個小時試圖解決這個問題。非常感謝! – Nicster15

回答

1

user: any = {};不是數組!嘗試user: any = [];

let o = {};這是一個對象。 let arr = [];這是一個數組。