I have the following problem. I want to subscribe to an observable using the async pipe in Angular. But the data in the pipe can originate from different data sources and the pipe can look different depending on the data source.
The way it is implemented now does not work, because I override the first value of this.data$ and thus source A never gets subscribed to.
Is it possible to 'split' the pipe depending on a filter?
problem.component.ts:
// Data source A and logic A
this.data$ = this.service.context.pipe(
filter(context => context.flag === true),
switchMap(context => this.dataSourceA(context.id))
map(data => this.processDataA(data))
);
// Data source B and logic B
this.data$ = this.service.context.pipe(
filter(context => context.flag === false),
switchMap(context => this.dataSourceB(context.id))
map(data => this.processDataB(data))
);
problem.component.html
<pro-table [data]="data$ | async"></pro-table>
if elsestatement