簡體   English   中英

在Angular 2中使用ngFor with Object Array時出錯

[英]Error using ngFor with Object Array in Angular 2

我在Component類中定義了一個對象數組,如下所示:

deskCategories : Array<any>;
this.deskCategories = [
        {
            catLabel : 'Tools',
            catIcon : 'suitcase'
        },
        {
            catLabel : 'Accounts',
            catIcon : 'table'
        },
        {
            catLabel : 'File Manager',
            catIcon : 'file'
        },
        {
            catLabel : 'Stock',
            catIcon : 'cubes'
        }
     ];

在我的模板中,我試圖將它與ngFor一起使用:

<div class="column" *ngFor="let cat of deskCategories">
    <div class="ui center aligned container">
        <i class="huge {{cat.catIcon}} icon link"></i>
        <p>{{cat.catLabel}}</p>
    </div>
</div>

列表顯示正常,但我收到一個控制台錯誤,說:

EXCEPTION:./DeskComponent類中的錯誤DeskComponent - 內聯模板:1:24 ORIGINAL EXCEPTION:TypeError:iterator1.next不是函數

請注意,它與字符串數組一起正常工作。 有什么我想念的嗎? 任何幫助,將不勝感激。 謝謝。

編輯:僅在Firefox和Chrome中獲取此錯誤(在Edge中正常工作!)

使用ngFor Angular 2時出錯

綁定時你沒有數組實例。

嘗試:

deskCategories : [];

constructor(){ //this could/should be done in ngOnInit but not super important now

this.deskCategories = [
    {
        catLabel : 'Tools',
        catIcon : 'suitcase'
    },
    {
        catLabel : 'Accounts',
        catIcon : 'table'
    },
    {
        catLabel : 'File Manager',
        catIcon : 'file'
    },
    {
        catLabel : 'Stock',
        catIcon : 'cubes'
    }
 ];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM