2016-08-04 85 views
3

我在使用從我的firebase數據庫中獲得的對象時出現問題。 我可以在沒有任何問題的情況下接收json文件,如下圖所示。 https://i.gyazo.com/6c1c69aca47f92a4e1029bb019042ab2.pngFirebase angularfire2從數據庫中獲取對象

<h1>{{ item | async | json}}</h1> 

上面的代碼是在/src/app/app.component.html,

此臨危從/src/app/app.component.ts項對象

import { Component } from '@angular/core'; 
import { AngularFire, FirebaseObjectObservable } from 'angularfire2'; 

@Component({ 
    moduleId: module.id, 
    selector: 'app-root', 
    templateUrl: 'app.component.html', 
    styleUrls: ['app.component.css'] 
}) 
export class AppComponent { 
    item: FirebaseObjectObservable<any>; 
    constructor(af: AngularFire) { 
     this.item = af.database.object('/releases/'); 
} 
} 

我也嘗試使用item.name。$ value,但它不起作用。我想要獲取json文件中的值。並且能夠在網站中使用它們。

回答

3

首先,你不需要一開始和對象搜索時結束斜槓,這將工作:

af.database.object('releases') 

接下來,你不需要JSON管,因爲火力的對象已經在JSON對象。你的HTML可以是這樣的:

<h1>{{ item | async }}</h1> 

但是,通常,而不是直接使用您的模板的火力異步對象,你會傳遞到演示文稿組件(也稱爲啞分量)。表示組件不需要知道任何關於對象的異步行爲,只需要處理如何生成html。處理模板中的異步對象時,這是一種常見模式。

所以,你的HTML代碼將變爲:

<my-child-component [item]="item | async"> 

的子組件:這裏所描述

@Component({ 
    selector: 'my-child-component', 
    template: '<h1>{{ item }}</h1>' 
}) 
export class MyChildComponent { 
    @Input() item: any; 
    ...