2017-03-28 93 views
2

我在火力地堡以下數據庫:多個對象火力地堡

Root Categories/ID/{text:Item1 AL:Item 2 TEXT:Item 3 ...} 

我有下面的代碼來獲得我想要「的.text」或具有先前文本的一個項目:

this.categories = af.database.list('/categories/' + this.ID); 
this.categories.subscribe(categories => console.log(categories)); 
this.categories.subscribe(
categories => { 
    categories.map(categories => 
     console.log(categories.text), 

    ) 
});' 

CONSOLE.LOG返回正是我想要的,但是當我在HTML檢索{{categories}}文件,它返回Object對象

有人知道如何解決它?對不起,我的無知我是一個初學者。

+0

嘗試通過指定'類別= JSON.parse(類別)'和嘗試檢查的'categories'使用'typeof運算(類別)' –

回答

0

如果您嘗試打印{{categories}}這是預期的行爲,因爲categories似乎是一個對象。如果您想看看categories正因爲如此,你需要添加json管:

{{categories | json}} 

但我懷疑,是不是你想要的。你需要使用屬性來顯示categories每個屬性,例如:

{{categories.text}} 

如果categories是數組,忽略上面的代碼,但JSON管會在這種情況下正常工作。如果categories是一個數組,那麼你需要遍歷數組,然後打印你從每個對象所需的屬性,如

<div *ngFor="let cat of categories"> 
    {{cat.text}} 
</div> 

如果要挑出一些特定的值,你可以做到這裏面的地圖並且將值賦給一個新的數組,例如:

categoriesText: string[] = []; 

this.categories.subscribe(
categories => { 
    categories.map(categories => { 
     console.log(categories.text) 
     this.categoriesText.push(categories.text); // push values to new array 
    }); 
}); 

然後你可以遍歷數組:

<div *ngFor="let cat of categoriesText"> 
    {{cat}} <!-- here no property name as it's only a string array --> 
</div> 

希望這有助於! :)

+0

完美型。完全按照我的預期工作。非常感謝 – lfelipe

+0

非常歡迎,很高興我能幫上忙! :) – Alex