Contenuti del Corso
Introduction to Angular
Introduction to Angular
Introduction to Pipes
We often need to display data in a way that's clear and visually appealing. This might include formatting names, dates, prices, percentages, or list items.
If you try to handle all these transformations manually inside the component logic, our templates can quickly become cluttered and messy. To solve this, Angular provides a clean and simple feature called pipes — a way to transform data directly in the template.
What Are Pipes?
Pipes in Angular are a convenient way to transform data directly in the template. They let you format or manipulate values like dates, numbers, or strings without touching your component's business logic.
Think of a pipe as a tool that takes in raw data, processes it, and outputs a modified version — kind of like a filter in a data stream.
Pipes help keep templates clean and readable by handling data formatting inline.
Using Pipes in Angular
Whenever you need to format data right inside the template, you can apply a pipe using the |
(pipe) symbol:
python
You can even chain multiple pipes together:
python
Some pipes also accept parameters, which you pass using colons (:
):
python
Angular comes with a set of built-in pipes to handle common formatting needs — from numbers and dates to currency and case transformations. Let's walk through real-world examples, starting with the basics and moving to more advanced use cases.
String Transformation
Let's say you have a property username
in your component. It's often useful to highlight or emphasize a user's name by displaying it in all uppercase letters.
template
component
<p>{{ userName | uppercase }}</p>
Here, the userName
property is pulled from the component and transformed to uppercase using Angular's built-in uppercase
pipe.
So if userName = 'alex'
, the output on the page will be: ALEX
.
Formatting Dates
Aside from strings, one of the most common data types you'll need to format is a date.
Imagine you have a user
object. You want to show the date they joined in a clean, readable format:
template
component
<p>Joined on: {{ user.dateJoined | date:'longDate' }}</p>
If user.dateJoined = new Date(2023, 3, 15)
, the result would be: Joined on: April 15, 2023
.
The date
pipe supports a wide range of formats, such as short
, medium
, fullDate
, or even custom formats like dd/MM/yyyy
.
Displaying Currency
Once you've got dates down, let's say you're building an online store.
You're working with a product
object. To show the price in a specific currency, use the currency
pipe:
template
component
<p>Price: {{ product.price | currency:'USD' }}</p>
If product.price = 199.99
, the output will be: Price: $199.99
.
You can customize the currency pipe with locale settings, display style, and whether or not to show the currency symbol.
There are many other useful pipes available in Angular. While we won't cover them all here, below is a quick reference list of pipes not yet mentioned:
To learn more about each pipe, visit the official Angular documentation.
1. What symbol is used to apply a pipe in an Angular template?
2. What will be the output of the following code if userName = 'anna'
:
Grazie per i tuoi commenti!