To elaborate on my comment, here an example of how you can achieve that by using a BehaviorSubject.
@Component({/*...*/})
export class YourComponent {
// This is the BehaviorSubject we will 'next' on, whenever preferredCurrency changed
// Replace 'initial' by your initial preferredCurrency
public preferredCurrency$:BehaviorSubject<string> = new BehaviorSubject('Initial');
private wmData$ = this.http.get<any>("./api/wealthModeling/GetWMData").pipe(
// Include this if your wmData is the same for all preferredCurrency (this will make it so that the HttpRequest is only performed once)
// shareReplay(1)
);
// pipe from your preferredCurrency$ subject - we will 'next' to it whenever a new preferredCurrency is selected, thus starting this chain
public portfolioCalculation$ = this.preferredCurrency$
.pipe(
// you don't need this, since BehaviorSubjects already start with a value (include if Observable without initial value is used instead)
// startsWith('initial'),
// We want to switch to the HTTP Request observable (whenever preferredCurrency was changed)
switchMap(currency => this.wmData$.pipe(
mergeMap(wmData => this.portfolioCalculationService.getPortfolioValues(wmData, this.preferredCurrency))
))
);
// Call this method (or run the statement inside directly) whenever your preferredCurrency changed
public changePreferredCurrency(newPreferredCurrency:string):void {
this.preferredCurrency$.next(newPreferredCurrency);
}
}
In place of the BehaviorSubject, any Observable that triggers whenever your preferredCurrency
changes can be used as a Source Observable in this case (e.g. if you are using @angular/forms, every control has a valueChanges
Observable). Just remember to incorporate a startsWith('initial')
operator as your first operator in your chain in case the Observable does not yield the initial value (e.g. @angular/forms valueChanges
does not yield the initial value).