【发布时间】:2019-06-19 14:18:31
【问题描述】:
我有以下两个订阅:
this.service1.source1.subscribe(countries => {
this.filteredData = [];
countries.forEach(country => {
this.filteredData = this.data.filter(user => {
return user.country == country;
});
});
});
this.service2.source2.subscribe(companies => {
companies.forEach(company => {
this.filteredData = [];
this.filteredData = this.data.filter(user => {
return user.company == company;
});
});
});
其中 source1 和 source2 是 BehaviorSubject 并且两个事件都被 subscribe 捕获。
我想合并两个订阅,因为我想根据subscribe 返回的两个结果过滤数据
我试过forkJoin和zip
zip([this.service1.source1, this.service1.source2]).subscribe(results => {
console.log('zip results:', results);
});
forkJoin([this.service1.source1, this.service1.source2]).subscribe(results => {
console.log('forkJoin results:', results);
});
但是对于那些(zip 和 forkJoin),我注意到我什至没有在控制台上得到任何东西,好像subscribe 在发出事件时没有被执行。
如何将两个来源的结果合并到一个订阅中?
【问题讨论】:
-
您是否尝试过从
forkJoin中删除数组?它接受可变参数来源,但我不确定这是否会有所不同。
标签: angular rxjs observable behaviorsubject subject