Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default parameters for angular pipes

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?

like image 365
baao Avatar asked Jun 08 '18 10:06

baao


People also ask

What are default pipes in Angular?

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 ).

Can we pass parameters to pipe in Angular?

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.

What does .pipe do in Angular?

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.

What is parameterized pipe in Angular?

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 (:).


1 Answers

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

like image 160
mostafa tourad Avatar answered Sep 21 '22 13:09

mostafa tourad