Bk Razor
Bk Razor

Reputation: 1532

How to listen for observable change inside the component

Right now I have an interval that checks if my observable changes every 1 second. I know this is not the most efficient way, but I can't figure out how to listen for the state change and run a function when it does, directly in the component. Here is my interval that I have now:

UPDATE 1: I am now using subscribe, but this is still not working for me. I am send a firebase object observable from an already subscribed observable. Is this where my problem lies?

Service

   private ReadyObs;

   SetRoom(){
   this.Room = this.AngularFire.database.object('/Rooms/ABC1');
    this.Room.subscribe(snap => {
        if(snap.$value !== null){
          this.Ready = this.AngularFire.database.object(`/Rooms/${player.room}/Ready`);
         }
      }

    checkReady(){
      return this.ReadyObs;
    }

Component

   private ReadyObs=this.main.checkReady();

   //Always results with still not ready even though its true
   this.ReadyObs.subscribe((result) =>{
    if(result === true)
      console.log("Ready to use");
     else
       console.log("Still not ready");
     });

Upvotes: 2

Views: 5172

Answers (1)

JayChase
JayChase

Reputation: 11525

If checkReady() is returning an Observable you can subscribe to it to get changes.

this.checkReady()
  .subscribe(result => {
    if (result === true) {
      console.log("Ready");
    } else {
      console.log("Still not ready");
    }
  }, (error) => {

  });

Upvotes: 1

Related Questions