【发布时间】:2018-07-16 01:58:24
【问题描述】:
说我有这个:
isMatchedCountLessThanTotalCountMessage(){
// I want to implement this
// "returns" a string asynchronously
}
getMatchedEventsCount() {
return this.dcs.matchCount.asObservable();
}
getTotalEventsCount() {
return this.dcs.totalCount.asObservable();
}
matchedCount 和 totalCount 是这样的:
public matchCount = new BehaviorSubject<number>(0);
public totalCount = new BehaviorSubject<number>(0);
这些 Observable 会随着值的变化而触发整数。每当一个值被其中一个触发时,我想比较两者中的两个最新值,我该怎么做?
我想做的是从方法中返回一个布尔值
所以我可以在 HTML 中显示:
<div>{{(isMatchedCountLessThanTotalCountMessage() | async)}}</div>
我认为 Observable.zip 可以解决问题:
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
});
}
【问题讨论】:
标签: rxjs rxjs5 angular2-observables behaviorsubject