0Pricing
Angular Academy · Lesson

Creating Custom Pipes

Build reusable transformation pipes.

Creating Custom Pipes is a free Angular Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Creating Custom Pipes” lesson free?

Yes — the full text of “Creating Custom Pipes” is free to read here on the web, and the Angular Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Angular Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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