Custom Pipe
In Angular, Pipes are powerful tools used to transform data directly within your HTML templates.1 While Angular provides many built-in pipes (like DatePipe, UpperCasePipe, and CurrencyPipe), there are often scenarios where you need specialized formatting logic unique to your application. This is where Custom Pipes come in.
Why Use Custom Pipes?
- Reusability: Write the logic once and use it across any component in your application.2
- Clean Templates: Keeps your HTML clean by moving complex transformation logic into a dedicated class.
- Performance: Angular optimizes pipes (especially Pure Pipes) to only re-run when the input data changes.3
Creating a Custom Pipe: Step-by-Step
To create a custom pipe, you need to implement the PipeTransform interface and use the @Pipe decorator.4
1. The Scenario
Imagine you are building a blog and want to show a "Reading Time" estimate based on the word count of an article.
2. The Implementation
Create a file named reading-time.pipe.ts:
Using the Pipe in a Component
Once defined, you need to import it into your component (if using standalone components) and apply it using the pipe operator (|) in your template.
Component Code:
Advanced Example: A Parameterized Truncate Pipe
Sometimes you want to shorten a long string (like a product description) and allow the developer to decide exactly how many characters to show and what symbol to use at the end (e.g., ... or Read More).
1. The Implementation
Create a file named truncate.pipe.ts. Notice how the transform method accepts additional arguments after the initial value.
2. Using Parameters in the Template
In Angular, you pass parameters to a pipe using the colon (:) symbol. You can chain as many parameters as the transform method is designed to handle.
Component Template:
How it Works
- First Argument: The value before the pipe operator (
longDescription) always maps to the first parameter of thetransformmethod. - Subsequent Arguments: Everything after the first colon maps to the second, third, and fourth parameters respectively.
- Type Safety: Because we are using TypeScript, if you try to pass a string where the pipe expects a number (like
truncate:'high'), the compiler will throw an error, helping you catch bugs early.
The Angular CLI (Command Line Interface) is the most efficient way to create pipes. It automates the "boilerplate" work by generating the necessary files and, if you aren't using standalone components, automatically declaring the pipe in your AppModule.
The Command
The basic syntax for generating a pipe is:
(Shortcut: ng g p pipe-name)
Step-by-Step Example: Creating a "File Size" Pipe
Let's say you want to create a pipe that converts a raw number of bytes into a human-readable format (like KB or MB).
1. Run the Command
Open your terminal in your project folder and run:
2. What the CLI Does
The CLI will perform two main actions:
- Creates
format-file-size.pipe.ts: A skeleton file with the@Pipedecorator andPipeTransforminterface already in place. - Creates
format-file-size.pipe.spec.ts: A unit test file so you can immediately start testing your logic. - Updates
AppModule/ Component: If you aren't using standalone mode, it adds the pipe to thedeclarationsarray of your module.
3. Add Your Logic
Open the newly generated format-file-size.pipe.ts and add the transformation logic:
4. Usage in Template
Now you can use the name defined in the decorator (formatFileSize) in any HTML file:
Useful CLI Flags
When generating pipes, you can use these flags to control the output:
| Flag | Description |
--flat | (Default: true) Prevents the CLI from creating a sub-folder for the pipe. |
--skip-tests | Skips generating the .spec.ts (unit test) file. |
--standalone | Ensures the pipe is generated as a Standalone component (default in newer Angular versions). |
--project | Specifies which project in a multi-project workspace to add the pipe to. |