簡體   English   中英

檢測到可觀察的Angular4中的更改?

[英]Detect change in Observable, Angular4?

我有一個FirebaseObjectObservable ,它將具有一個名為game_state的屬性,當它更改時,我希望某些方法能夠在我的組件中觸發,並且我的google-foo必須是弱函數,因為我似乎找不到我想要的東西。

編輯:我只是想更新此內容,因為沒有人收到與實際代碼問題相關的響應,但是LifeCycle Hooks是我一直在尋找的東西。

我解決此問題的方法是使用構造函數提供服務:

constructor(private db: AngularFireDatabase) {}

和方法:

getObject(key: string): FirebaseObjectObservable<any> {
    return this.db.object('/objects/' + key);
}

在您的組件中:

constructor( private yourService: YourService){}

ngOnInit(){
      this.yourService.getObject(key).subscribe(item => {
      console.log(` Updated - call your methods here `);
      });
}

希望對您有幫助,對我來說這是很新的方法,但是這種方法對我有用。 很想知道是否有更好的方法。

暫無
暫無

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

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