简体   繁体   中英

Detect change in Observable, Angular4?

I have a FirebaseObjectObservable that will have a property called game_state that, when it changes, I want certain methods to trigger in my component, and my google-foo must be weak because I can't seem to find what I'm looking for.

EDIT: I just wanted to update this since no one had a response related to the actual code problem, but LifeCycle Hooks are what I was looking for.

The way I have solved this is to have a service with constructor:

constructor(private db: AngularFireDatabase) {}

And a method:

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

In your component:

constructor( private yourService: YourService){}

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

Hope that helps, it is pretty new to me but this method worked for my purposes. Would love to know if there is a better way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM