倾听可观察值的变化

时间:2018-10-15 14:33:24

标签: typescript ionic-framework observable

我正在尝试将Ionic的loadcontroller与firestore查询一起使用。据我所知,这返回一个可观察的值,并且还监听查询值的变化。

但是在函数中是否有一种方法可以知道可观察对象的值是否已更改?因为现在即使没有任何更改,加载程序也会在每次单击时显示,所以我希望加载程序仅在查询结果更改时才显示。

这就是我想做的

category(category){
  let loading = this.load.create({
    content: 'Please wait...'
  });



  this.selected = category;

    this.fsp.getCategory(category).subscribe(res => {
      if(res has changed){
         loading.present();
      this.data = this.group.groupEvents(res);
         loading.dismiss();
       } else(observable value remained the same) {
           this.data = this.group.groupEvents(res);
           }
    });

  }

1 个答案:

答案 0 :(得分:0)

我不确定,因为我已经很长时间没有使用Firestore了,但是 也许您可以使用valueChanges()来获取“当前”值,并将其存储在上一个变量中,并且每次订阅被触发时,进行比较并采取相应的措施。