In Angular, pipes are a powerful way to transform data in your templates. Built-in pipes provide a variety of common data transformations, such as formatting dates, numbers, and strings. This section will cover the most commonly used built-in pipes and how to use them effectively in your Angular applications.
Key Concepts
-
What are Pipes?
- Pipes are simple functions that accept an input value, process it, and return a transformed value.
- They are used in Angular templates to format data before displaying it to the user.
-
Syntax
- Pipes are used in templates with the pipe (
|) character. - Example:
{{ value | pipeName }}
- Pipes are used in templates with the pipe (
-
Common Built-in Pipes
DatePipeUpperCasePipeLowerCasePipeCurrencyPipeDecimalPipePercentPipeJsonPipeSlicePipe
Using Built-in Pipes
DatePipe
The DatePipe is used to format date values according to locale rules.
Syntax:
Example:
Explanation:
todayis a date object.'fullDate'is a predefined format option.
UpperCasePipe and LowerCasePipe
These pipes are used to transform text to uppercase or lowercase.
Syntax:
Example:
<p>{{ 'hello world' | uppercase }}</p> <!-- Outputs: HELLO WORLD -->
<p>{{ 'HELLO WORLD' | lowercase }}</p> <!-- Outputs: hello world -->CurrencyPipe
The CurrencyPipe formats a number as currency.
Syntax:
Example:
Explanation:
1234.56is the number to be formatted.'USD'specifies the currency code.'symbol'specifies that the currency symbol should be displayed.'1.2-2'specifies the number of integer and fraction digits.
DecimalPipe
The DecimalPipe formats a number as a decimal.
Syntax:
Example:
Explanation:
1234.5678is the number to be formatted.'1.2-2'specifies the number of integer and fraction digits.
PercentPipe
The PercentPipe formats a number as a percentage.
Syntax:
Example:
Explanation:
0.1234is the number to be formatted.'1.2-2'specifies the number of integer and fraction digits.
JsonPipe
The JsonPipe converts a value into a JSON string.
Syntax:
Example:
SlicePipe
The SlicePipe creates a new array or string containing a subset (slice) of the elements.
Syntax:
Example:
<p>{{ [1, 2, 3, 4, 5] | slice:1:3 }}</p> <!-- Outputs: [2, 3] -->
<p>{{ 'Hello World' | slice:0:5 }}</p> <!-- Outputs: Hello -->Practical Exercise
Exercise 1: Formatting Dates
Task:
Create an Angular component that displays the current date in different formats using the DatePipe.
Solution:
-
Create a new component:
ng generate component date-format -
Update the component template (
date-format.component.html):<p>Full Date: {{ today | date:'fullDate' }}</p> <p>Short Date: {{ today | date:'shortDate' }}</p> <p>Medium Time: {{ today | date:'mediumTime' }}</p> -
Update the component class (
date-format.component.ts):import { Component } from '@angular/core'; @Component({ selector: 'app-date-format', templateUrl: './date-format.component.html', styleUrls: ['./date-format.component.css'] }) export class DateFormatComponent { today: Date = new Date(); }
Exercise 2: Using CurrencyPipe
Task:
Create an Angular component that displays a product price in different currencies using the CurrencyPipe.
Solution:
-
Create a new component:
ng generate component product-price -
Update the component template (
product-price.component.html):<p>Price in USD: {{ price | currency:'USD':'symbol' }}</p> <p>Price in EUR: {{ price | currency:'EUR':'symbol' }}</p> <p>Price in GBP: {{ price | currency:'GBP':'symbol' }}</p> -
Update the component class (
product-price.component.ts):import { Component } from '@angular/core'; @Component({ selector: 'app-product-price', templateUrl: './product-price.component.html', styleUrls: ['./product-price.component.css'] }) export class ProductPriceComponent { price: number = 1234.56; }
Summary
In this section, we explored the built-in pipes provided by Angular, including DatePipe, UpperCasePipe, LowerCasePipe, CurrencyPipe, DecimalPipe, PercentPipe, JsonPipe, and SlicePipe. We learned how to use these pipes to transform data in our templates and practiced with practical exercises to reinforce the concepts. Understanding and utilizing these built-in pipes will help you format and display data effectively in your Angular applications.
Angular 2+ Course
Module 1: Introduction to Angular
Module 2: TypeScript Basics
- Introduction to TypeScript
- TypeScript Variables and Data Types
- Functions and Arrow Functions
- Classes and Interfaces
