比较来自多个 BehaviorSubjects 的最新值 [英] Compare most recent values from multiple BehaviorSubjects

查看:68
本文介绍了比较来自多个 BehaviorSubjects 的最新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有这个:

 isMatchedCountLessThanTotalCountMessage(){//我想实现这个//异步返回"一个字符串}getMatchedEventsCount() {返回 this.dcs.matchCount.asObservable();}getTotalEventsCount() {返回 this.dcs.totalCount.asObservable();}

matchedCount 和 totalCount 是这样的:

 public matchCount = new BehaviorSubject(0);public totalCount = 新的 BehaviorSubject(0);

这些 Observables 会随着值的变化而触发整数.任何时候从任何一个中触发一个值,我都想比较两个最近的值,我该怎么做?

我想要做的是从方法返回一个布尔值

所以我可以在 HTML 中显示:

 

{{(isMatchedCountLessThanTotalCountMessage() | async)}}

我认为 Observable.zip 可能会起作用:

isMatchedCountLessThanTotalCountMessage(){返回 Observable.zip(this.getMatchedEventsCount(),this.getTotalEventsCount()).subscribe(function(v){const intA = v[0];const intB = v[1];如果(intA > intB)//但我不知道如何从这里发送 HTML 消息});}

解决方案

您可以轻松使用 .map() 函数来转换您想要的数据:

isMatchedCountLessThanTotalCountMessage() {返回 Observable.combineLatest(this.getMatchedEventsCount(),this.getTotalEventsCount(),).map(([intA, intB]) => {返回 intA >内部?'(结果被过滤)' : '(结果未被过滤)'})}

Say I have this:

  isMatchedCountLessThanTotalCountMessage(){
       // I want to implement this
       // "returns" a string asynchronously
  }

  getMatchedEventsCount() {
    return this.dcs.matchCount.asObservable();
  }

  getTotalEventsCount() {
    return this.dcs.totalCount.asObservable();
  }

matchedCount and totalCount are like so:

  public matchCount = new BehaviorSubject<number>(0);
  public totalCount = new BehaviorSubject<number>(0);

these Observables fire integers as values change. Anytime a value is fired from either one, I want to compare the two most recent values from both, how do I do that?

What I want to do is return a boolean from the method

so I can display in the HTML:

 <div>{{(isMatchedCountLessThanTotalCountMessage() | async)}}</div>

I think Observable.zip might do the trick:

isMatchedCountLessThanTotalCountMessage(){
    return Observable.zip(
      this.getMatchedEventsCount(),
      this.getTotalEventsCount()
    )
    .subscribe(function(v){
      const intA = v[0];
      const intB = v[1];

        if(intA > intB)
         // but I don't know how to send a message the HTML from here
    });
  }

解决方案

You can easily use .map() function to transform the data you want:

isMatchedCountLessThanTotalCountMessage() {
    return Observable.combineLatest(
        this.getMatchedEventsCount(),
        this.getTotalEventsCount(),
    )
        .map(([intA, intB]) => {
            return intA > intB ? '(results ARE filtered)' : '(results are not filtered)'
        })
}

这篇关于比较来自多个 BehaviorSubjects 的最新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆