Learn Custom Pipes in Angular 17

Angular, a popular framework for building dynamic web applications, provides a robust set of built-in tools and functionalities to enhance the development process. Among these tools, pipes play a crucial role in transforming data within templates. While Angular offers several built-in pipes (like DatePipe, CurrencyPipe, UpperCasePipe, etc.), there are times when you may need to create custom pipes to meet specific requirements. This article will guide you through the process of creating custom pipes in Angular 17.

What Are Pipes in Angular?

Pipes in Angular are a simple way to transform data displayed in the template. They are defined using the | (pipe) character and can be used to format data such as dates, numbers, or strings directly within the template without modifying the component's class.

For example, to format a date, you can use the DatePipe.

<p>{{ today | date: 'fullDate' }}</p>

Creating Custom Pipes

Creating a custom pipe in Angular involves three main steps.

  1. Generating the pipe.
  2. Implementing the transformation logic.
  3. Registering the pipe.

Step 1. Generating the Pipe

Angular CLI provides a convenient command to generate a new pipe. Open your terminal and run the following command.

ng generate pipe custom

his command will generate two files.

  • custom.pipe.ts
  • custom.pipe.spec.ts

Step 2. Implementing the Transformation Logic

Open the generated custom.pipe.ts file and implement the transformation logic. Below is an example of a custom pipe that converts a string to a title case (i.e., the first letter of each word is capitalized).

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
  name: 'titleCase'
})
export class TitleCasePipe implements PipeTransform {
  transform(value: string): string {
    if (!value) {
      return value;
    }
    return value
      .split(' ')
      .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
      .join(' ');
  }

}

Step 3. Registering the Pipe

For the pipe to be available throughout your Angular application, you need to declare it in an Angular module. Open your module file (e.g., app.module.ts) and add the pipe to the declarations array.

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { TitleCasePipe } from './title-case.pipe';
@NgModule({
  declarations: [
    AppComponent,
    TitleCasePipe
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Using the Custom Pipe

With the custom pipe implemented and registered, you can now use it in your templates. For instance, to convert a string to a title case, you would use the titleCase pipe as follows.

<p>{{ 'angular custom pipe' | titleCase }}</p>

Output

Angular Custom Pipe

Advanced Custom Pipes

Custom pipes can also accept parameters to provide more flexibility. For example, let's create a custom pipe that truncates a string to a specified length and adds an ellipsis (...) if the string exceeds that length.

First, generate the pipe.

ng generate pipe truncate

Next, implement the transformation logic in truncate.pipe.ts.

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
  name: 'truncate'
})
export class TruncatePipe implements PipeTransform {
  transform(value: string, limit: number): string {
    if (!value) {
      return value;
    }
    if (value.length <= limit) {
      return value;
    }
    return value.slice(0, limit) + '...';
  }

}

Finally, register the pipe in your module and use it in a template.

<p>{{ 'This is a long string that needs to be truncated' | truncate:20 }}</p>

Output

This is a long stri...

Conclusion

Creating custom pipes in Angular 17 allows you to extend the framework's capabilities and tailor data transformation to your specific needs. By following the steps outlined in this article, you can easily create and implement custom pipes to enhance your Angular applications. Whether it's formatting text, manipulating numbers, or transforming dates, custom pipes provide a powerful way to handle data presentation in Angular.