
- Angular Tutorial
- Angular - Home
- Angular - Overview
- Angular - Features
- Angular - Advantages & Disadvantages
- Angular Basics
- Angular - Environment setup
- Angular - First Application
- Angular - MVC Architecture
- Angular Components
- Angular - Components
- Angular - Component Lifecycle
- Angular - View Encapsulation
- Angular - Component Interaction
- Angular - Component Styles
- Angular - Nested Components
- Angular - Content projection
- Angular - Dynamic components
- Angular - Elements
- Angular Templates
- Angular - Templates
- Angular - Template statements
- Angular - Template Variables
- Angular - SVG as Templates
- Angular Binding
- Angular - Data Binding
- Angular - Interpolation
- Angular - Event Binding
- Angular - Property Binding
- Angular - Attribute Binding
- Angular - Class Binding
- Angular - Style Binding
- Angular - Two-way Binding
- Angular Directives
- Angular - Directives
- Angular - Attribute Directives
- Angular - Structural Directives
- Angular - Custom Directives
- Angular Pipes
- Angular - Pipes
- Angular - Built-in Pipes
- Angular - Custom Pipes
- Angular Forms
- Angular - Forms
- Angular - Template Driven Forms
- Angular - Reactive Forms
- Angular - Form Validation
- Angular - Dynamic Forms
- Angular Dependency Injection
- Angular - Dependency Injection
- Angular - Injectable Service
- Angular Routing
- Angular - Routing
- Angular - Dynamic Routes
- Angular - Wildcard Routes
- Angular - Nested Routes
- Angular - Navigation
- Angular - Routing in SPA
- Angular - Custom Route Matches
- Angular - Router Reference
- Angular HTTP Client programming
- Angular - Services
- Angular - HTTP Client
- Angular - Request
- Angular - Response
- Angular - GET
- Angular - POST
- Angular - PUT
- Angular - DELETE
- Angular - JSONP
- Angular - CRUD Operations Using HTTP
- Angular Modules
- Angular - Introduction to Modules
- Angular - Root Module
- Angular - Feature Module
- Angular - Sharing Module
- Angular - Routing Module
- Angular - NgModules
- Angular Animation
- Angular - Animations
- Angular Service Workers & PWA
- Angular - Service Workers & PWA
- Angular Testing
- Angular - Testing Overview
- Angular Design Patterns
- Angular - Design Patterns
- Angular - Lazy Loading
- Angular - Singleton Pattern
- Angular - Observer Pattern
- Angular Libraries
- Angular - Libraries
- Angular - Angular Material
- Angular - PrimeNG
- Angular - RxJS
- Angular Advanced
- Angular - Signals
- Angular - Authentication & Authorization
- Angular - Internationalization
- Angular - Standalone Component
- Angular - Accessibility
- Angular - Web Workers
- Angular - Server Side Rendering
- Angular - Ivy Compiler
- Angular - Building with Bazel
- Angular - Backward Compatibility
- Angular - Reactive Programming
- Angular Tools
- Angular - CLI
- Angular Material UI Elements
- Angular - Paginator
- Angular - Datepicker
- Angular - Select Drop-down
- Angular Miscellaneous
- Angular - Third Party Controls
- Angular - Configuration
- Angular - Displaying Data
- Angular - Decorators & Metadata
- Angular - Basic Example
- Angular - Error Handling
- Angular - Testing & Building a Project
- Angular - Lifecycle Hooks
- Angular - User Input
- Angular - What's New?
- Angular Useful Resources
- Angular - Quick Guide
- Angular - Useful Resources
- Angular - Discussion
Angular - Custom Pipes
Custom Pipes in Angular
A custom pipe is a user-defined pipe that allows developers to perform specific transformations that Angular's built-in pipes can't achieve. The built-in pipes provide limited functionalities like formatting dates, numbers and strings. However, you can achieve more than that using custom pipes. For example, sorting and filtering.
In Angular, pipes are functions that format specified data before displaying it in the View.
Features and Uses of Angular Custom Pipes
The Angular Custom Pipes provides the following features −
- Once you define a custom pipe, you can reuse it wherever needed in the application.
- Like built-in pipes, custom pipes can also be used directly in the template expressions.
- The logic for custom pipes is written in a separate TypeScript class and applied within the template. This helps in maintaining the code and improving the performance.
- There are a number of features that you can add using custom pipes to your Angular application, such as digit count, filtering, sorting, password generation and many more.
Creating a Custom Pipe in Angular
Follow the steps given below to create a Custom pipe in Angular −
Step 1: Create a TypeScript class and export it. By convention, a pipe class should end with the string "Pipe".
Step 2: Decorate the created TypeScript class with @Pipe Decorator. Inside this decorator, specify a name for the pipe.
Step 3: In the end, inherit the PipeTransform interface and implement its transform() method.
Example
The following example explains about creating a custom Pipe and using it inside an Angular application.
Step 1: Open Angular CLI and generate a Pipe using the below command −
ng g pipe digitcount
After executing the above command, you can see these two files −
CREATE src/app/digitcount.pipe.spec.ts (211 bytes) CREATE src/app/digitcount.pipe.ts (258 bytes)
Step 2: Let's create a logic for counting digits in a number using Pipe. Open digitcount.pipe.ts file and add the below code −
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'digitcount' }) export class DigitcountPipe implements PipeTransform { transform(val : number) : number { return val.toString().length; } }
Step 3: Navigate to app.component.html file and use the newly created pipe inside the template as shown below −
<div> <h3> Using Custom DigitCount Pipe </h3> <p>Count :- {{ digits | digitcount }}</p> </div> <router-outlet />
Step 4: Now, we have added logic to count number of digits in a given number. It's time to add the final code in app.component.ts file. Here, we import the DigitcountPipe and define a number.
import { Component } from '@angular/core'; import { RouterOutlet } from '@angular/router'; import { DigitcountPipe } from './digitcount.pipe'; @Component({ selector: 'app-root', standalone: true, imports: [RouterOutlet, DigitcountPipe], templateUrl: './app.component.html', styleUrl: './app.component.css' }) export class AppComponent { digits : number = 100; }
Step 5: Now, run the application using ng serve command. The code will display the following result on the browser −
Using Custom DigitCount Pipe Count :- 3