【问题标题】:How to listen for observable change inside the component如何监听组件内部的可观察到的变化
【发布时间】:2016-12-17 06:32:54
【问题描述】:

现在我有一个间隔来检查我的 observable 是否每 1 秒发生一次变化。我知道这不是最有效的方法,但我不知道如何直接在组件中监听状态变化并在它发生时运行函数。这是我现在的间隔:

更新 1: 我现在正在使用订阅,但这仍然不适合我。我从一个已经订阅的 observable 发送一个可观察的 firebase 对象。这是我的问题所在吗?

服务

   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;
    }

组件

   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");
     });

【问题讨论】:

    标签: angular rxjs observable


    【解决方案1】:

    如果 checkReady() 返回一个 Observable,您可以订阅它以获取更改。

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

    【讨论】:

    • 我正在尝试这个,但由于某种原因这对我不起作用,我将更新我的代码
    猜你喜欢
    • 2021-05-15
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    相关资源
    最近更新 更多