📖 Scenario: You are building a simple Angular app that displays a list of product prices. You want to show these prices formatted as currency with a dollar sign and two decimals.
🎯 Goal: Create a custom Angular pipe called currencyFormat that formats numbers as US dollars with two decimal places. Use this pipe in the template to display the formatted prices.
📋 What You'll Learn
Create an array called prices with the exact numbers: 10, 23.5, 99.99, 5
Create a string variable called currencySymbol with the value '$'
Create a custom pipe class called CurrencyFormatPipe that implements PipeTransform
Use the currencyFormat pipe in the template to display each price with the currency symbol and two decimals
💡 Why This Matters
🌍 Real World
Custom pipes help format data like prices, dates, or text consistently across an Angular app.
💼 Career
Knowing how to create and use custom pipes is a common skill for Angular developers to improve UI data presentation.
Progress0 / 4 steps
1
Set up the prices array
Create an array called prices with these exact numbers: 10, 23.5, 99.99, and 5.
Angular
Hint
Use square brackets [] to create the array and separate numbers with commas.
2
Add the currency symbol variable
Create a string variable called currencySymbol and set it to '$'.
Angular
Hint
Use single quotes around the dollar sign to make it a string.
3
Create the custom pipe class
Create a custom pipe class called CurrencyFormatPipe that implements PipeTransform. Inside, write a transform method that takes a number and returns a string with the currencySymbol followed by the number formatted with two decimals (use toFixed(2)).
Angular
Hint
Use the @Pipe decorator with name: 'currencyFormat'. The transform method formats the number.
4
Use the custom pipe in the template
In the Angular template, use *ngFor="let price of prices" to loop over prices. Inside the loop, display each price using the currencyFormat pipe like this: {{ price | currencyFormat }}.
Angular
Hint
Use *ngFor on an <li> element inside a <ul>. Use interpolation with the pipe.
Practice
(1/5)
1. What is the main purpose of creating a custom pipe in Angular?
easy
A. To manage component lifecycle hooks
B. To handle HTTP requests
C. To define routing paths
D. To transform data in templates for display
Solution
Step 1: Understand the role of pipes in Angular
Pipes are used to transform data before displaying it in templates.
Step 2: Identify what custom pipes do
Custom pipes let you create your own data transformations to keep templates clean and reusable.
Final Answer:
To transform data in templates for display -> Option D
Quick Check:
Custom pipes = transform data [OK]
Hint: Pipes change how data looks in templates [OK]
Common Mistakes:
Confusing pipes with services
Thinking pipes handle HTTP or routing
Mixing pipes with component lifecycle
2. Which of the following is the correct way to define a custom pipe class in Angular?
easy
A. export class MyPipe implements PipeTransform { transform(value: any): any { return value; } }
B. export class MyPipe { transform(value: any): any { return value; } }
C. export function MyPipe(value: any): any { return value; }
D. export class MyPipe implements OnInit { transform(value: any): any { return value; } }
Solution
Step 1: Recall Angular pipe class requirements
A custom pipe class must implement the PipeTransform interface and define a transform method.
Step 2: Check each option
export class MyPipe implements PipeTransform { transform(value: any): any { return value; } } correctly implements PipeTransform and has transform method. export class MyPipe { transform(value: any): any { return value; } } misses interface. export function MyPipe(value: any): any { return value; } is a function, not a class. export class MyPipe implements OnInit { transform(value: any): any { return value; } } wrongly implements OnInit instead of PipeTransform.
Final Answer:
export class MyPipe implements PipeTransform { transform(value: any): any { return value; } } -> Option A
Quick Check:
Pipe class implements PipeTransform [OK]
Hint: Custom pipes must implement PipeTransform interface [OK]
Common Mistakes:
Forgetting to implement PipeTransform
Using a function instead of a class
Implementing wrong interfaces like OnInit
3. Given this custom pipe code, what will be the output of {{ 'hello' | exclaim }} if the pipe is defined as:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'exclaim' })
export class ExclaimPipe implements PipeTransform {
transform(value: string): string {
return value + '!';
}
}
medium
A. Error: Pipe not found
B. hello
C. hello!
D. !hello
Solution
Step 1: Understand the transform method behavior
The transform method appends an exclamation mark to the input string.
Step 2: Apply the pipe to the input 'hello'
Input 'hello' becomes 'hello!' after transformation.
Final Answer:
hello! -> Option C
Quick Check:
Input + '!' = hello! [OK]
Hint: transform method output is what pipe shows [OK]
Common Mistakes:
Ignoring the transform method logic
Assuming pipe adds prefix instead of suffix
Not registering the pipe in module (causing errors)
A. The class does not implement PipeTransform interface
B. The transform method is missing
C. The pipe name is invalid
D. The split method cannot be used on strings
Solution
Step 1: Check class declaration
The class ReversePipe does not implement PipeTransform interface, which is required for custom pipes.
Step 2: Verify transform method presence
The transform method exists and is correctly implemented.
Final Answer:
The class does not implement PipeTransform interface -> Option A
Quick Check:
Pipe class must implement PipeTransform [OK]
Hint: Always implement PipeTransform for custom pipes [OK]
Common Mistakes:
Omitting PipeTransform interface
Misnaming the transform method
Incorrect pipe decorator usage
5. You want to create a custom pipe that formats a number to show 'Even' if the number is even, 'Odd' if odd, and 'Zero' if zero. Which transform method implementation is correct?
hard
A. transform(value: number): string { return value % 2 === 0 ? 'Even' : 'Odd'; }
B. transform(value: number): string { if (value === 0) return 'Zero'; return value % 2 === 0 ? 'Even' : 'Odd'; }
C. transform(value: number): string { if (value === 0) return 'Odd'; else return 'Even'; }
D. transform(value: number): string { if (value) return 'Even'; else return 'Zero'; }
Solution
Step 1: Handle zero case explicitly
Zero is a special case and must return 'Zero'.
Step 2: Check even or odd for other numbers
Use modulo operator to check if number is even or odd and return accordingly.
Final Answer:
transform(value: number): string { if (value === 0) return 'Zero'; return value % 2 === 0 ? 'Even' : 'Odd'; } -> Option B
Quick Check:
Zero check first, then even/odd [OK]
Hint: Check zero before even/odd to avoid wrong output [OK]
Common Mistakes:
Not handling zero separately
Returning wrong labels for zero
Using incorrect conditions for even/odd
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.