Angular2 Pipe to convert currency

随声附和 提交于 2019-12-11 00:59:09

问题


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 ?


回答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;
  }
}

register it with a modules declarations and use it like

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


来源:https://stackoverflow.com/questions/42267303/angular2-pipe-to-convert-currency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!