I've been working on a pipe that converts currency values from one to another by making an HTTP call. The idea is to pass in the source and destination currencies as parameters.
@Pipe({
name: "currencyConverter"
})
export class CurrencyConverterPipe implements PipeTransform {
transform(value: number, currencyPair: string): number {
let sourceCurrency = currencyPair.split(":")[0];
let destinationCurrency = currencyPair.split(":")[1];
this.currencyConverterService.convert(sourceCurrency, destinationCurrency).subscribe(successData => {
return successData['fiatValue'] * value;
}, errorData => {
console.error(errorData);
})
}
}
When using it in HTML:
<p> {{ value | currencyConverter:'BTC:USD'}}</p>
However, I'm facing a problem where no value is being displayed on the UI. If I set my pipe to impure
, it starts working but results in multiple HTTP calls to the server. Is there a way to make the HTTP call without having to use pure: false
in the pipe?