I am looking to combine 3 events and trigger a service call when any of them are fired. Currently, I am using the combineLatest
method, but it seems to only work when the first event is triggered by filterChanged
.
The issue here is that filterChanged
is a local event, while the other two events come from a child component. How can I modify this setup so that even if one of the events is emitted during page initialization, it will still trigger the service?
filterChanged = new EventEmitter<boolean>();
paginatorChanged = new EventEmitter<MatPaginator>();
sortChanged = new EventEmitter<{}>();
ngAfterViewInit(): void {
combineLatest(
this.paginatorChanged,
this.sortChanged,
this.filterChanged
).pipe(
startWith([null, null, null]),
switchMap(value => {
const paginator = value[0];
const sort = value[1];
const filters = value[2];
return this.service.getFiltered(paginator, sort, filters);
})).subscribe(data => this.data = data);
}
applyFilter(): void {
this.filterChanged.emit(true);
}
onPaginatorChanged(paginator): void {
this.paginatorChanged.emit(paginator);
}
onSortChanged(sort): void {
this.sortChanged.emit(sort);
}
Thank you!