为什么在使用管道时不在html中评估值?

时间:2017-07-31 10:41:16

标签: angular pipe subscribe

我正在使用管道进行货币转换,不会在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服务获取费率并在管道内订阅?我怎样才能改变它以使其有效?

1 个答案:

答案 0 :(得分:2)

缺少return

return this.currencyConversion.getExchangeRate(...

并添加| async管道:

<span> {{listitem.value  | inclCommission  | currConvert:selectedCurrency:listitem.currency | async}}</span>

<强>更新

subscribe(...)需要更改为.map(...)