0
我想使用角2與firebase和我已經設法推送數據到firebase和得到的後面,但是當我嘗試向firebase添加新的數據他們不會反映在.get可觀察到的自動 - 必須刷新頁面或在路由之間切換,這是正確的行爲?如果不是,我應該怎麼做,以自動更新新的價值觀,因爲他們來了?可觀察不發射新值
import { Component, OnInit } from "angular2/core";
import { FirebaseService } from "../shared/firebase.service";
import { ArcItem } from "./arc-item.model";
import { Observable } from "rxjs/Observable";
import { KeysPipe } from "../shared/keys.pipe";
@Component({
selector: "arc-list",
template: `
<ul class="arc-list">
<li *ngFor="#item of listItems | async | keys" class="arc-item">
<h3>{{ item.name}}</h3><a [href]="item.resourceUrl" target="_blank" class="btn btn-success pull-right"><span>Go</span></a>
<hr>
<blockquote>{{ item.description }}</blockquote>
<hr>
</li>
</ul>
`,
pipes: [KeysPipe]
})
export class ArcListComponent implements OnInit {
listItems: Observable<any[]>;
constructor(private _firebaseService: FirebaseService) {}
ngOnInit(): any {
this.listItems = this._firebaseService.getResources();
}
}
firebase.service
import { Injectable } from "angular2/core";
import { Http } from "angular2/http";
import { ArcItem } from "../arc/arc-item.model";
import "rxjs/Rx";
@Injectable()
export class FirebaseService {
constructor(private _http: Http) {}
setResource(id: number, title: string, description: string, resourceUrl: string) {
const body = JSON.stringify({ id: id, title: title, description: description, resourceUrl: resourceUrl});
return this._http
.post("https://#####.firebaseio.com/resource.json", body)
.map(response => response.json());
}
getResources() {
return this._http
.get("https://#####.firebaseio.com/resource.json")
.map(response => response.json());
}
}
有沒有沒有Firebase庫的代碼版本?因爲我只是使用firebase的其餘api。 –
事實上,如果沒有這個,當你收到'setResource'的響應時(在'setResource'返回的observable中訂閱的回調函數中),你需要明確調用'getResources'方法...希望我能正確理解你的使用案件。 –
我會嘗試,謝謝你,但每次調用getResources都不會獲取所有數據而不是隻添加一個或? –