Angular2 Pipe转换货币 [英] Angular2 Pipe to convert currency

查看:155
本文介绍了Angular2 Pipe转换货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一种使用api转换货币的方法,

I have created a method to convert currency using a api, which looks as follows,

 exchange(Input: string, Output: string, value: number): number {
        let inputRate = this.currencyStorage.getCurrencyRate(cnput);
        let outputputRate = this.currencyStorage.getCurrencyRate(Output);
        return value/ inputRate * outputputRate;
    }

我如何从中创建一个可在整个应用程序中使用的管道转换货币?

How can i create a pipe out of this which can be used throughout the application to convert currency ?

推荐答案



@Pipe({name: 'currConvert'})
export class CurrConvertPipe implements PipeTransform {
  constructor(private currencyStorage:MyCurrencyStorage) {}

  transform(value: number, Input: string, Output: string): number {
    let inputRate = this.currencyStorage.getCurrencyRate(cnput);
    let outputputRate = this.currencyStorage.getCurrencyRate(Output);
    return value/ inputRate * outputputRate;
  }
}

使用模块注册它声明,并像这样使用它

register it with a modules declarations and use it like

{{123 | currConvert:456 /*input*/:789 /*output*/}}

这篇关于Angular2 Pipe转换货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆