我试图合并两个Observable并根据其中一个或两个的结果发出一个事件。
export class MyDirective {
// Filter properties
filter = new ReplaySubject<any>();
}
export class MyComponent implements AfterContentInit {
// First observable (my own creation)
@ContentChildren(MyDirective) columns: MyDirective[];
filterChange = Observable.merge(...this.columns.map(c => c.filter));
// Second observable (EventEmitter from a component library)
@ViewChild(MdSort) sort: MdSort;
ngAfterContentInit() {
// Now merge them and call a function with the results from both
Observable.xxx([ this.sort.sortChange, this.filterChange ]).subscribe(res => {
this.readData(res.sort, res.filter);
});
}
readData(sort?: any, filter?: any) {
// This method should receive the results from the observables
}
}
我尝试过不同的东西来合并它们:
Observable.merge
Observable.merge(this.sort.sortChange, this.filterChange).subscribe(res => {
// Gives me no clue as to which of the observables has been fired
// and thus I cannot process properties
});
Observable.zip
Observable.zip(this.sort.sortChange, this.filterChange, (sort, filter) => {
// I never reach this spot. Why?
this.readData(sort, filter);
});
订阅
这有效,但我对此并不满意。我希望这可以使用Observable以某种方式合并...
// On any changes, read data
let sort; let filter;
this.sort.sortChange.subscribe(s => sort = s);
this.filterChange.subscribe(f => filter = f);
this.Observable.merge(this.sortChange, this.filterChange).subscribe(result => {
// Fired whenever any of the two are changed
this.readData(sort, filter);
});
答案 0 :(得分:1)
您可以distinctUntilchanged()
与map()
或mapTo()
一起使用,为您提供有关哪个流已发布值的信息
this.Observable.merge(this.sortChange.distinUntilChanged().mapTo('sort'),
this.filterChange.distinUntilChanged().mapTo('filter').subscribe(result => {
// Fired whenever any of the two are changed
return this.readData(sort, filter);
});
取决于sortchange
或filterChange
是否发出单个值,您可能必须应用自定义比较器逻辑才能使distinctUntilchanged起作用。 E.g
var source = Rx.Observable.of({value: 42}, {value: 42}, {value: 24}, {value: 24})
.distinctUntilChanged(function (x) { return x.value; }, function (a,b) {
return a !== b; });