5

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? } } 
1
  • 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). Commented Feb 15, 2017 at 18:50

1 Answer 1

1

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); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

@tuliomarchetto actually extend is not general/good solution - especially when you want to use more pipes inside one pipe.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.