4

I want to make a custom currency pipe using the built-in currency pipe. The way I want to use is {{ anyNumber | customCurrency }}. Then inside my customCurrency pipe, I want to use built-in currency pipe. I can decide the parameters to currency pipe based on some logic and don't want to specify locale and other parameters everywhere like {{anyNumber | currency:'USD':false:'1:0-0'}}.

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'customCurrency'
})
export class CustomCurrencyPipe implements PipeTransform {

  transform(value: any, args?: any): any {
      const defaultLocale = 'USD';
      const showSymbol = false;

      ......some logic here......      

      //HOW TO USE CURRENCY PIPE HERE?

 }

}
Vijay Malik
  • 107
  • 1
  • 1
  • 10
  • 1
    Either use dependency injection or, as the currency pipe is stateless, just `new` one up (you can `@Inject(LOCALE_ID)` to pass as its constructor argument). – jonrsharpe Feb 15 '17 at 18:50

1 Answers1

0

You should inject the built-in pipe into your custom pipe than you can call it's transform method with your default values

@Pipe({
  name: 'customCurrency'
})
export class CustomCurrencyPipe implements PipeTransform {

  constructor(public currencyPipe: CurrencyPipe) {
  }

  transform(value: any, args?: any): any {
      const currencyCode = 'USD';
      const showSymbol = false;
      return currencyPipe.transform(value, currencyCode, showSymbol);
 }

}
akos.bordas
  • 179
  • 1
  • 7