为什么在使用管道时不在 html 中计算值?
Why the values are not evaluated in the html when using pipe?
我正在使用管道进行货币转换,值未在 HTML 中计算。
这是我的烟斗
transform(value: number, selectedCurrency: string, baseCurrency: string): number {
if (selectedCurrency && baseCurrency) {
let outputRate = this.currencyStorage.getCurrencyRate(baseCurrency, selectedCurrency);
if (outputRate !== null) {
this.currencyConversion.getExchangeRate(selectedCurrency, baseCurrency).subscribe((rate: any) => {
let currency = { 'fromCurrency': baseCurrency, 'toCurrency': selectedCurrency, 'exchangeRate': rate };
this.currencyStorage.saveCurrencies(currency);
return value * rate;
}, (error: any) => this.errorMessage = error);
} else {
return value * outputRate;
}
}
};
和我的HTML
<span> {{listitem.value | inclCommission | currConvert:selectedCurrency:listitem.currency}</span>
是因为我使用 api 服务获取费率并在管道内订阅吗?我该如何更改它才能使其正常工作?
少了一个return
return this.currencyConversion.getExchangeRate(...
并添加 | async
管道:
<span> {{listitem.value | inclCommission | currConvert:selectedCurrency:listitem.currency | async}}</span>
更新
subscribe(...)
需要改为.map(...)
我正在使用管道进行货币转换,值未在 HTML 中计算。
这是我的烟斗
transform(value: number, selectedCurrency: string, baseCurrency: string): number {
if (selectedCurrency && baseCurrency) {
let outputRate = this.currencyStorage.getCurrencyRate(baseCurrency, selectedCurrency);
if (outputRate !== null) {
this.currencyConversion.getExchangeRate(selectedCurrency, baseCurrency).subscribe((rate: any) => {
let currency = { 'fromCurrency': baseCurrency, 'toCurrency': selectedCurrency, 'exchangeRate': rate };
this.currencyStorage.saveCurrencies(currency);
return value * rate;
}, (error: any) => this.errorMessage = error);
} else {
return value * outputRate;
}
}
};
和我的HTML
<span> {{listitem.value | inclCommission | currConvert:selectedCurrency:listitem.currency}</span>
是因为我使用 api 服务获取费率并在管道内订阅吗?我该如何更改它才能使其正常工作?
少了一个return
return this.currencyConversion.getExchangeRate(...
并添加 | async
管道:
<span> {{listitem.value | inclCommission | currConvert:selectedCurrency:listitem.currency | async}}</span>
更新
subscribe(...)
需要改为.map(...)