Pipes are often used for formatting dates, numbers, and Strings and they can be customized to suit various needs. In this article, we explain about how to use pipes in Angular with examples and related outputs for your reference.
Prerequisites:
To understand this article you should have knowledge of the following.
Pipes in Angular
The Pipes in Angular are powerful features that transform data directly in your template. They take in data as input and transform it into the desired output. Angular comes with several built-in pipes such as DatePipe, UpperCasePipe, LowerCasePipe, CurrencyPipe, and DecimalPipe.
Syntax:
{{ today | date:'fullDate' }}
Commonly Used Pipes in Angular
Below we provide built in and mostly used pipes in angular.
- DatePipe: Formats a date value according to locale rules
- UpperCasePipe: Transforms text to uppercase
- LowerCasePipe: Transforms text to lowercase
- CurrencyPipe: Formats a number as currency.
- DecimalPipe: Formats a number as decimal.
- PercentPipe: Formats a number as a percentage.
- SlicePipe: Slice a String or Array and return a new sub string or sub array.
- JsonPipe: Converts a value into its JSON String representation.
- TitleCasePipe: Capitalizes the first letter of each word.
- KeyValuePipe: This pipe is used for transform an object or a Map into an array of key value pairs.
Steps to Create the Application
Step 1: Install Angular CLI
Open your terminal and run:
npm install -g @angular/cli
Step 2: Create a New Angular Project
ng new pipes
Step 3: Navigate to Your Project Directory
cd pipes
Project Structure:
Folder StructureThe updated dependencies in the package.json file are:
"dependencies": {
"@angular/animations": "^18.0.0",
"@angular/common": "^18.0.0",
"@angular/compiler": "^18.0.0",
"@angular/core": "^18.0.0",
"@angular/forms": "^18.0.0",
"@angular/platform-browser": "^18.0.0",
"@angular/platform-browser-dynamic": "^18.0.0",
"@angular/platform-server": "^18.0.0",
"@angular/router": "^18.0.0",
"@angular/ssr": "^18.0.0",
"express": "^4.18.2",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
}
Here we provide syntax and example and outputs for each pipe mention in the above list.
DatePipe
The DatePipe is used for Formats a date value according to locale rules.
Syntax:
<p>{{ currentDate | date: 'fullDate' }}</p>
Example: Add the below mentioned code in app.component.ts and app.component.html file.The below HTML code holds the pipe for the currentDate. TS file holds the data about component being standalone and imports holds the imports made for the particular component.
HTML
<!-- app.component.html -->
<p>{{ currentDate | date: 'fullDate' }}</p>
JavaScript
// app.component.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, CommonModule],
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
currentDate = new Date();
title = 'Pipes';
}
Output
Output of DatePipeUpperCasePipe
This UpperCasePipe is used for transform text to uppercase
Syntax:
<p>{{ 'hello world' | uppercase }}</p>
Example: Add the below mentioned code in app.component.ts and app.component.html file.The below HTML code holds the pipe for the Uppercase. TS file holds the data about component being standalone and imports holds the imports made for the particular component.
HTML
<!-- app.component.html -->
<p>{{ 'geeksforgeeks' | uppercase }}</p>
JavaScript
// app.component.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, CommonModule],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent {
title = 'Pipes';
}
Output
Output of UpperCasePipeLowerCasePipe
This LowerCasePipe is used for Transforms text to lowercase.
Syntax:
<p>{{ 'GEEKSFORGEEKS' | lowercase }}</p>
Example: Add the below mentioned code in app.component.ts and app.component.html file. The below HTML code holds the pipe for the LowerCase. TS file holds the data about component being standalone and imports holds the imports made for the particular component.
HTML
<!-- app.component.html --->
<p>{{ 'GEEKSFORGEEKS' | lowercase }}</p>
JavaScript
//app.component.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, CommonModule],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent {
title = 'Pipes';
}
Output
Output of LowerCasePipeCurrencyPipe
The CurrencyPipe is used for Formats a number as currency.
Syntax:
<p>{{ 12345.6789 | currency: 'INR' }}</p>
Example: Add the below mentioned app.component.ts and app.component.html file. The below HTML code holds the pipe for the Currency. TS file holds the data about component being standalone and imports holds the imports made for the particular component.
HTML
<!--app.component.html --->
<p>{{ 12345.6789 | currency: 'INR' }}</p>
JavaScript
// app.component.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, CommonModule],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent {
title = 'Pipes';
}
Output
Output of CurrencyPipeDecimalPipe
The DecimalPipe is used for Formats a number as a decimal.
Syntax:
<p>{{ 12345.6789 | number: '1.2-2' }}</p>
Example: Add the below mentioned app.component.ts and app.component.html file. The below HTML code holds the pipe for the Decimal Number. TS file holds the data about component being standalone and imports holds the imports made for the particular component.
HTML
<!---app.component.html--->
<p>{{ 12345.6789 | number: '1.2-2' }}</p>
JavaScript
// app.component.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, CommonModule],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent {
title = 'Pipes';
}
Output
Output of DecimalPipePercentPipe
The PercentPipe is used for Formats a number as a percentage.
Syntax:
<p>{{ 0.6789 | percent: '1.2-2' }}</p>
Example: Add the below mentioned app.component.ts and app.component.html file. The below HTML code holds the pipe for the Percent. TS file holds the data about component being standalone and imports holds the imports made for the particular component.
HTML
<!--app.component.html-->
<p>{{ 0.6789 | percent: '1.2-2' }}</p>
JavaScript
//app.component.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, CommonModule],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent {
title = 'Pipes';
}
Output
Output of PercentPipeSlicePipe
The SlicePipe is used for Slices a string or array and returns a new sub string or sub array.
Syntax:
for String:
<p>{{ 'Angular Pipes' | slice: 0:7 }}</p>
for Array:
<p>{{ [1, 2, 3, 4, 5] | slice: 1:3 }}</p>
Example: Add the below mentioned app.component.ts and app.component.html file. The below HTML code holds the pipe for the Slice. TS file holds the data about component being standalone and imports holds the imports made for the particular component.
HTML
<!--app.component.html-->
<p>{{ 'GeeksForGeeks' | slice: 0:7 }}</p>
<p>{{ [1, 2, 3, 4, 5] | slice: 1:3 }}</p>
JavaScript
// app.component.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, CommonModule],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent {
title = 'Pipes';
}
Output
Output of SlicePipeJsonPipe
The JsonPipe is used for Converts a value into its JSON string representation.
Syntax:
<p>{{ { name: 'John', age: 30 } | json }}</p>
Example: Add the below mentioned code in app.component.html and app.component.ts file.The below HTML code holds the pipe for the JSON Format. TS file holds the data about component being standalone and imports holds the imports made for the particular component.
HTML
<!--app.component.html --->
<p>{{ { name: 'John', age: 30 } | json }}</p>
JavaScript
//app.component.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, CommonModule],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent {
title = 'Pipes';
}
Output
Output of JsonPipeTitleCasePipe
The TitleCasePipe is used for Capitalizes the first letter of each word.
Syntax:
<p>{{ 'welcome to geeksforgeeks' | titlecase }}</p>
Example: Add the below mentioned app.component.ts and app.component.html file.The below HTML code holds the pipe for the TitleCase. TS file holds the data about component being standalone and imports holds the imports made for the particular component.
HTML
<!-- app.component.html --->
<p>{{ 'welcome to geeksforgeeks' | titlecase }}</p>
JavaScript
// app.component.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, CommonModule],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent {
title = 'Pipes';
}
Output
Output of TitleCasePipeKeyValuePipe
The KeyValuePipe is used for Transforms an object or a Map into an array of key value pairs.
Syntax:
<div *ngFor="let item of object | keyvalue">
Key: {{ item.key }}, Value: {{ item.value }}
</div>
Example: Add the below mentioned app.component.ts and app.component.html file. The below HTML code holds the pipe for the KeyValue. TS file holds the data about component being standalone and imports holds the imports made for the particular component.
HTML
<!-- app.component.html -->
<div *ngFor="let item of object | keyvalue">
Key: {{ item.key }}, Value: {{ item.value }}
</div>
JavaScript
// app.component.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, CommonModule],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent {
title = 'Pipes';
object = { id: 1, name: 'GeeksForGeeks' };
}
Output
Output of KeyValuePipeConclusion
Angular pipes are versatile tools for transforming data in your template they help keep your code clean and declarative making it easier to format and display data. Whether you are using built in pipes understanding how to leverage pipes will enhance the flexibility and reliability of your Angular application.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read