【问题标题】:Angular2 Pipe to convert currencyAngular2管道转换货币
【发布时间】:2017-07-05 04:07:33
【问题描述】:

我创建了一个使用api转换货币的方法,如下所示,

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

我怎样才能创建一个可以在整个应用程序中用于转换货币的管道?

【问题讨论】:

    标签: angular filter pipe currency


    【解决方案1】:
    @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;
      }
    }
    

    使用模块declarations 注册它并像使用它一样

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

    【讨论】:

    • {{123 | currConvert:EUR:USD}}
      我喜欢这个,它说未定义
    • 我猜你的意思是`
      {{123 | currConvert:'EUR':'USD'}}
      ` 文字字符串需要引号。
    • 一个问题,不刷新页面就改变参数,管道会起作用吗?
    • 每次值或参数更改时都会执行管道 - 是的,无需刷新页面。
    • @GünterZöchbauer 我应该点击 Api 来获取管道内的货币汇率吗?请提出建议。
    猜你喜欢
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 2017-06-02
    • 1970-01-01
    相关资源
    最近更新 更多