Is there any way (other than creating my own pipe that later uses angular's) to set application wide defaults for an angular pipe? I'm extensively using the currency pipe like this
{{ price | currency:'EUR':'symbol':'0.2-2':'de' }}
I don't want to always write all the parameters :'EUR':'symbol':'0.2-2':'de'
and would rather set them once for all usages of currency
.
I don't find something about that in the docs - is it possible or do I need my own pipe with defaults?
By default, pipes are defined as pure so that Angular executes the pipe only when it detects a pure change to the input value. A pure change is either a change to a primitive input value (such as String , Number , Boolean , or Symbol ), or a changed object reference (such as Date , Array , Function , or Object ).
You can easily pass multiple arguments in pipe in angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13 and angular 14 application. In this example we will create 'descPipe' custom pipe and create dynamic description with multiple parameters.
Pipes are a useful feature in Angular. They are a simple way to transform values in an Angular template. There are some built in pipes, but you can also build your own pipes. A pipe takes in a value or values and then returns a value.
In an angular application, when we pass any parameters to the pipes, it is called parameterized pipes we can pass n number of parameters to pipes using a colon (:).
Most of angular pipes use existing methods to return their transform value , for example if you take a look at the currency pipe you'll see that it uses two method to return the string value of the formatted numer to currency , formatCurrency and getCurrencySymbol and they are available in angular common module so we can use them to build our own version of the currency pipe without the need of inheritance or something like that , simply the currency pipe return the return value of formatCurrency function and call it with the params passed to the pipe and uses getCurrencySymbol to get the symbol of a specified currency code
so now we can build our own version with default values we specify and here it is
import { Pipe, PipeTransform } from '@angular/core';
import { formatCurrency, getCurrencySymbol } from '@angular/common';
@Pipe({
name: 'mycurrency',
})
export class MycurrencyPipe implements PipeTransform {
transform(
value: number,
currencyCode: string = 'EUR',
display:
| 'code'
| 'symbol'
| 'symbol-narrow'
| string
| boolean = 'symbol',
digitsInfo: string = '0.2-2',
locale: string = 'en-US',
): string | null {
return formatCurrency(
value,
locale,
getCurrencySymbol(currencyCode, 'wide'),
currencyCode,
digitsInfo,
);
}
}
Now its working perfect
Note here I am using en-US as locale if you need to use other locale such as de you need to import it and register,
more info
angular pipes
CurrencyPipe
formatCurrency
getCurrencySymnol
angular common
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With