Site icon Tutorial

Built-in Pipes

Go back to Tutorial

Angular comes with a stock of pipes such as DatePipe, UpperCasePipe, LowerCasePipe, CurrencyPipe, and PercentPipe. They are all available for use in any template.

CurrencyPipe

This pipe is used for formatting currencies. Its first argument is an abbreviation of the currency type (e.g. “EUR”, “USD”, and so on), like so:

{{ 1234.56 | currency:’CAD’ }}

The above prints out CA$1,234.56.

If instead of the abbreviation of CA$ we want the currency symbol to be printed out we pass as a second parameter the string symbol-narrow, like so:

{{ 1234.56 | currency:”CAD”:”symbol-narrow” }}

The above prints out $1,234.56.

Its syntax is as –

number_expression | currency[:currencyCode[:display[:digitInfo[:locale]]]]

Description

Use currency to format a number as currency.

DatePipe

Its syntax is as

date_expression | date[:format[:timezone[:locale]]]

Description

Where:

DecimalPipe

This pipe is used for transformation of decimal numbers. The first argument is a format string of the form “{minIntegerDigits}. {minFractionDigits}-{maxFractionDigits}”.

Its syntax is as

number_expression | number[:digitInfo[:locale]]

Formats a number as text. Group sizing and separator and other locale-specific configurations are based on the active locale.

where expression is a number:

LowerCasePipe

It transforms text to lowercase, as example below

@Component({

selector: ‘lowerupper-pipe’,

template: `<div>

<label>Name: </label><input #name (keyup)=”change(name.value)” type=”text”>

<p>In lowercase: <pre>'{{value | lowercase}}'</pre>

<p>In uppercase: <pre>'{{value | uppercase}}'</pre>

</div>`

})

export class LowerUpperPipeComponent {

value: string;

change(value: string) { this.value = value; }

}

JsonPipe

Its syntax is as

expression | json

Description – Converts value into string using JSON.stringify. Useful for debugging.

Example

@Component({

selector: ‘json-pipe’,

template: `<div>

<p>Without JSON pipe:</p>

<pre>{{object}}</pre>

<p>With JSON pipe:</p>

<pre>{{object | json}}</pre>    </div>` })

export class JsonPipeComponent {

object: Object = {foo: ‘bar’, baz: ‘qux’, nested: {xyz: 3, numbers: [1, 2, 3, 4, 5]}};

}

Go back to Tutorial

Exit mobile version