Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Creating a Custom Pipe in Angular | Section
Angular Fundamentals

bookCreating a Custom Pipe in Angular

Sveip for å vise menyen

Sometimes, the built-in pipes in Angular just aren't enough. You might need a unique transformation that's specific to your app. In that case, you can create your own custom pipe.

Let's say you have a list of tasks, and each task can be either completed or not. To make completed tasks visually stand out, you want to automatically add a ✅ emoji to their titles. Instead of hardcoding this logic into every template, you can build a dedicated pipe for it.

How to Create a Custom Pipe

To generate a custom pipe in Angular, use the following CLI command. We'll call it formatTitle, since its job is to format the task title based on its completion status:

This will create two files:

  • format-title.pipe.ts — the pipe logic;

  • format-title.pipe.spec.ts — a unit test file (you can delete it if you don't need testing right now).

When Angular generates the pipe, it looks something like this:

pipe.ts

pipe.ts

copy

Here's what this code does:

  • The FormatTitlePipe class implements PipeTransform, which ensures the pipe has a transform method;

  • The transform method receives a value (the input) and optional arguments and returns a transformed result. At this point, it just returns the input as-is;

  • The pipe's name (formatTitle) is defined in the @Pipe decorator — that's the name you'll use in the template.

Right now, the pipe exists but doesn't actually do anything. Let's add the actual logic next.

Custom Pipe Implementation

We need to add an ✅ emoji to the title based on the task's completion status (task.completed). Update the pipe to apply our custom formatting logic:

pipe.ts

pipe.ts

copy

The transform method takes two arguments:

  • value — the task title;

  • completed — a boolean indicating whether the task is done.

If the task is complete (true), it adds a ✅ emoji at the end of the title. Otherwise, it just returns the title unchanged.

Using the Pipe in a Template

To use the pipe in your Angular template, apply it with the pipe symbol |:

template.html

template.html

copy

What's happening here:

  • task.title is the value passed to the pipe;

  • formatTitle:task.completed applies the pipe and sends in the completed flag;

  • The title will either include the ✅ emoji or not, depending on the task's status.

Using pipes like this helps keep your templates clean and readable, and pushes the formatting logic into reusable, testable code.

question mark

What does the formatTitle custom pipe do?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 18

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Seksjon 1. Kapittel 18
some-alt