Transforming Data Using Pipes in Angular
Last Updated :
05 Sep, 2024
In Angular, Pipes are a powerful feature that allows you to transform data directly in your templates. Pipes are like helpers that make it easy to change the way data looks on your webpage without writing a lot of code. Whether you need to format dates, manipulate strings, or perform complex calculations pipes offer a simple way to handle these tasks efficiently.
This article will guide you through the concept of pipes in Angular, explore different approaches to using them, and provide practical examples to help you implement them in your applications.
Prerequisites
Approach
1. Built-in Pipes
Angular comes with several built-in pipes that you can use right out of the box. Here’s how you can use a few of them:
<p>{{ today | date:'fullDate' }}</p>
2. UpperCasePipe: Transforms text to uppercase.
<p>{{ 'hello world' | uppercase }}</p>
<p>{{ amount | currency:'USD':true }}</p>
2. Custom Pipes
Sometimes, built-in pipes aren’t enough. Angular allows you to create your own custom pipes to meet specific requirements.
To create a custom pipe, use the Angular CLI:
ng generate pipe custom
Example of a custom pipe to reverse a string:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'reverse'
})
export class ReversePipe implements PipeTransform {
transform(value: string): string {
return value.split('').reverse().join('');
}
}
Use it in your template:
<p>{{ 'Angular' | reverse }}</p>
3. Chaining Pipes
You can chain multiple pipes to perform complex data transformations:
<p>{{ 'angular pipes' | uppercase | reverse }}</p>
This will first transform the string to uppercase and then reverse it.
Steps To Transforming Data Using Pipes
Now, let's create an Angular application where we implement these pipes.
Step 1: Install Angular CLI
If you haven’t installed Angular CLI yet, you can install it using the following command:
npm install -g @angular/cli
Step 2: Create a New Angular Project
Generate a new Angular project:
ng new pipes-demo
cd pipes-demo
Step 3: Generate Components and Pipes
Let’s generate the components and custom pipes:
ng generate component example
ng generate pipe reverse
Folder Structure
Folder StructureDependencies
"dependencies": {
"@angular/animations": "^18.2.0",
"@angular/common": "^18.2.0",
"@angular/compiler": "^18.2.0",
"@angular/core": "^18.2.0",
"@angular/forms": "^18.2.0",
"@angular/platform-browser": "^18.2.0",
"@angular/platform-browser-dynamic": "^18.2.0",
"@angular/platform-server": "^18.2.0",
"@angular/router": "^18.2.0",
"@angular/ssr": "^18.2.0",
"express": "^4.18.2",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.10"
}
Step 4: Implement Pipes in Components
Implement the built-in and custom pipes in your example.component.html:
<p>Original: {{ 'Angular Pipes' }}</p>
<p>Uppercase: {{ 'Angular Pipes' | uppercase }}</p>
<p>Reversed: {{ 'Angular Pipes' | reverse }}</p>
<p>Chained: {{ 'Angular Pipes' | uppercase | reverse }}</p>
Example: Below is the example demonstrating the transformation of data using Pipes in Angular.
Example Component: HTML and TS code of example component showing pipes such as uppercase, reverse and uppercase reverse and commonmodule and reversepipe in imports array.
HTML
<!-- src/app/example/example.component.html -->
<div>
<h2>Angular Pipes Example</h2>
<p>Original: {{ 'Angular Pipes' }}</p>
<p>Uppercase: {{ 'Angular Pipes' | uppercase }}</p>
<p>Reversed: {{ 'Angular Pipes' | reverse }}</p>
<p>Chained: {{ 'Angular Pipes' | uppercase | reverse }}</p>
</div>
JavaScript
//src/app/example/example.component.ts
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { ReversePipe } from '../reverse.pipe';
@Component({
selector: 'app-example',
standalone: true,
imports: [CommonModule, ReversePipe],
templateUrl: './example.component.html',
styleUrl: './example.component.css'
})
export class ExampleComponent {
}
App Component: Below is the code example demonstrating app.component.html having selector of example component and example component and router outlet are added as imports in imports array.
HTML
<!-- src/app/app.component.html -->
<app-example></app-example>
JavaScript
//src/app/app.component.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { ExampleComponent } from "./example/example.component";
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, ExampleComponent],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent {
title = 'pipes-demo';
}
Reverse.pipe.ts: Below mentioned is a pipe named as reverse pipe which is a standalone component used for transforming.
JavaScript
//src/app/reverse.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'reverse',
standalone: true,
})
export class ReversePipe implements PipeTransform {
transform(value: string): string {
return value.split('').reverse().join('');
}
}
To start the Application run the following command.
ng serve --open
Output:
Transforming Data Using Pipes in Angular
Similar Reads
News App Using Angular
We will be developing a News Application using the Angular framework. This interactive app will allow users to search for news articles and filter them by category. With a clean and responsive design, it will dynamically display articles, providing an engaging and user-friendly experience. The app w
9 min read
Angular10 animation transition API
In this article, we are going to see what is transition in Angular 10 and how to use it. The transition in Angular10 is used to create a transition for the animation in which an element will go from one state to another. Syntax: transition (stateChangeExpr, steps, options) NgModule: Module used by t
2 min read
Angular PrimeNG Inplace Data
Angular PrimeNG is a UI component library for Angular Applications. It offers many pre-built themes and UI components for a variety of tasks like inputs, menus, charts, Buttons, etc. In this article, we will be seeing Angular PrimeNG Inplace Data. The Inplace component is used to provide an easy to
3 min read
Pass Data Between Siblings in Angular
In Angular, components are the building blocks of the user interface. If the structure of components is hierarchical, passing data between parent and child components is simple using input/output bindings. However, passing data directly between sibling components can be more challenging since they d
5 min read
How to Use the Async Pipe in Angular?
The AsyncPipe in Angular is a powerful and convenient tool used to handle asynchronous data streams such as observables and promises directly in the component template. It automatically subscribes to observables, renders their values, and updates the view when new data is emitted. This removes the n
3 min read
Joke generator App Using Angular
Angular has become one of the most popular frameworks for building dynamic web applications. In this project, we build a simple yet entertaining Joke generator app using Angular. The app will fetch random jokes from a public API and display them to the user. This project will help us understand how
3 min read
Quiz App using Angular
We will be creating a Quiz application using the Angular framework. This app will allow users to answer multiple-choice questions and view their results interactively. We'll use Angular Material for a polished UI and implement responsive design to ensure it looks great on all devices. The applicatio
5 min read
String Interpolation in Angular 8
String Interpolation in Angular 8 is a One-way Data-Binding technique that is used to transfer the data from a TypeScript code to an HTML template (view). It uses the template expression in double curly braces to display the data from the component to the view. String interpolation adds the value of
2 min read
Building Complex Forms Using Angular Reactive Form
Imagine you are building a web page where you have to make a Form having fields, such as First Name, Last Name, and so on and you have to handle form inputs, validations, and interactions. Here, using Reactive Forms you define the structure of the form directly in Typescript code rather than HTML te
7 min read
AngularJS Fetch Data From API using HttpClient
There is some data in the API and our task here is to fetch data from that API using HTTP and display it. In this article, we will use a case where the API contains employee details which we will fetch. The API is a fake API in which data is stored in the form of a JSON (Key: Value) pair. API stands
4 min read