Angular Academy · Lesson

Creating Custom Pipes

Build reusable transformation pipes.

Lesson 3 of 413 steps

Creating Custom Pipes is a free Angular Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, and your progress syncs across the web and the CoddyKit app. The Angular Academy course includes 4 lessons in total.

Why Custom Pipes?

When built-in pipes are not enough, you can create a custom pipe to encapsulate reusable transformation logic for your templates.

The @Pipe Decorator

A pipe is a class decorated with @Pipe. The name property is what you use in templates.

import { Pipe } from "@angular/core";

@Pipe({ name: "exclaim", standalone: true })
export class ExclaimPipe {}

Implementing PipeTransform

A pipe class implements the PipeTransform interface, which requires a single transform method.

import { Pipe, PipeTransform } from "@angular/core";

@Pipe({ name: "exclaim", standalone: true })
export class ExclaimPipe implements PipeTransform {
  transform(value: string): string {
    return value + "!";
  }
}

The transform Method

The first parameter of transform is the piped value. Whatever you return becomes the rendered output.

transform(value: string): string {
  return value.trim();
}

Using Your Pipe

Import the pipe into a standalone component, then use its name in the template just like a built-in pipe.

@Component({
  selector: "app-demo",
  standalone: true,
  imports: [ExclaimPipe],
  template: "<p>{{ 'hi' | exclaim }}</p>"
})
export class DemoComponent {}
// renders: hi!

Accepting Arguments

Extra parameters after value become pipe arguments. Callers pass them with colons.

@Pipe({ name: "repeat", standalone: true })
export class RepeatPipe implements PipeTransform {
  transform(value: string, times: number): string {
    return value.repeat(times);
  }
}
// {{ "ab" | repeat:3 }} -> ababab

Default Argument Values

Give arguments default values so callers can omit them. This keeps your pipe flexible.

transform(value: string, times: number = 2): string {
  return value.repeat(times);
}
// {{ "ab" | repeat }} -> abab

Handling Null and Undefined

Always guard against missing input. Return a sensible fallback when the value is null or undefined.

transform(value: string | null): string {
  if (value == null) return "";
  return value.toUpperCase();
}

A Truncate Pipe Example

Here is a practical pipe that shortens long text and appends an ellipsis.

@Pipe({ name: "truncate", standalone: true })
export class TruncatePipe implements PipeTransform {
  transform(value: string, limit: number = 20): string {
    if (!value) return "";
    return value.length > limit
      ? value.slice(0, limit) + "..."
      : value;
  }
}

Typing the Return Value

Annotate the return type so TypeScript and your templates stay type-safe. A pipe can return any type, not just strings.

@Pipe({ name: "double", standalone: true })
export class DoublePipe implements PipeTransform {
  transform(value: number): number {
    return value * 2;
  }
}

Registering in a Module (NgModule)

If you use NgModules instead of standalone components, declare the pipe in declarations and export it if needed.

@NgModule({
  declarations: [TruncatePipe],
  exports: [TruncatePipe]
})
export class SharedModule {}

Quick Check

Test your understanding of custom pipes.

Recap

You built a custom pipe with @Pipe and PipeTransform, accepted arguments with defaults, guarded against null, and registered it in standalone components or NgModules.

Next you will learn about pure versus impure pipes.

Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
38
Lessons
144

Frequently Asked Questions

Is the “Creating Custom Pipes” lesson free?

Yes — any 3 lessons of the Angular Academy learning path, including “Creating Custom Pipes”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Angular Academy course includes 4 lessons in total.

What will I learn in “Creating Custom Pipes”?

Build reusable transformation pipes. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Angular Academy?

No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Creating Custom Pipes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Angular Academy lesson?

Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Using Built-in Pipes
  2. Chaining and Parameterizing Pipes
  3. Creating Custom Pipes
  4. Pure vs Impure Pipes
← Back to Angular Academy