Angular Tutorial 120 lessons · 2:23:08+ of video
Angular Home What Angular Is, Where Angular Is Used and How Angular Works
What Angular Is Where Angular Is Used How Angular Works What You Need, Creating a Project and The Project Structure
What You Need Creating a Project The Project Structure The Root Component, ng serve and Your First Change
The Root Component ng serve Your First Change Why TypeScript, Basic Types and Interfaces
Why TypeScript Basic Types Interfaces Classes, Optional and Union Types and Arrays and Generics
Classes Optional and Union Types Arrays and Generics Decorators, import and export and Practice: Typed Books
Decorators import and export Practice: Typed Books Components, Templates and Interpolation
Components Templates Interpolation Property Binding, Event Binding and ng generate component
Property Binding Event Binding ng generate component Using Components Together, Component Styles and Practice: A Book Card
Using Components Together Component Styles Practice: A Book Card What Signals Are, signal() and set() and update()
What Signals Are signal() set() and update() computed(), effect() and Signals with Arrays
computed() effect() Signals with Arrays Signals with Objects, linkedSignal() and Practice: A Counter and a Basket
Signals with Objects linkedSignal() Practice: A Counter and a Basket @if, @else and @else if and @for
@if @else and @else if @for track, @empty and @switch
track @empty @switch @let, Class and Style Bindings and Practice: A Book List
@let Class and Style Bindings Practice: A Book List Parent and Child, input() and Required Inputs and Defaults
Parent and Child input() Required Inputs and Defaults output(), Sending Data Up and model() for Two-Way Binding
output() Sending Data Up model() for Two-Way Binding Content Projection, Building a Small Component Tree and Practice: A Star Rating
Content Projection Building a Small Component Tree Practice: A Star Rating Pipes, Built-in Pipes and Pipe Arguments
Pipes Built-in Pipes Pipe Arguments Custom Pipes, Directives and Custom Attribute Directives
Custom Pipes Directives Custom Attribute Directives host Listeners, Template Reference Variables and Practice: A Price List
host Listeners Template Reference Variables Practice: A Price List Why Services, Creating a Service and inject()
Why Services Creating a Service inject() @Service and @Injectable, Sharing State in a Service and Signals in Services
@Service and @Injectable Sharing State in a Service Signals in Services Providers, Injection Tokens and Practice: A Wishlist Service
Providers Injection Tokens Practice: A Wishlist Service What Routing Is, Routes and router-outlet and routerLink
What Routing Is Routes and router-outlet routerLink Route Parameters, Navigating in Code and Redirects and Not Found
Route Parameters Navigating in Code Redirects and Not Found Lazy Loading, Route Guards and Practice: Book Pages
Lazy Loading Route Guards Practice: Book Pages Forms in Angular, Template-Driven Forms and Reactive Forms
Forms in Angular Template-Driven Forms Reactive Forms FormGroup, Validators and Showing Errors
FormGroup Validators Showing Errors Signal Forms, Custom Validators and Practice: A Signup Form
Signal Forms Custom Validators Practice: A Signup Form Talking to a Server, HttpClient and GET Requests
Talking to a Server HttpClient GET Requests Observables Basics, POST, PUT and DELETE and Loading and Errors
Observables Basics POST, PUT and DELETE Loading and Errors httpResource, Interceptors and Practice: A Books Client
httpResource Interceptors Practice: A Books Client RxJS Basics, map and filter and Search with debounceTime
RxJS Basics map and filter Search with debounceTime toSignal(), Why Test and Testing a Component
toSignal() Why Test Testing a Component Testing a Service, Testing User Actions and Practice: Test the Basket
Testing a Service Testing User Actions Practice: Test the Basket Building for Production, @defer and Deploying
Building for Production @defer Deploying Project: The Plan, Models and Service and The Book List
Project: The Plan Project: Models and Service Project: The Book List Project: The Book Page, The Basket and Checkout Form
Project: The Book Page Project: The Basket Project: Checkout Form Project: Tests, Where to Go Next and Course Recap
Project: Tests Where to Go Next Course Recap Angular · Lesson 58 of 120
Custom Pipes Your own formatting.
⏱ 2 min read ▶ Video at 1:08:14 🎓 Free
▶ starts at 1:08:14 Our shop shows stock as a number. But customers want words, like sold out, or only two left. No built-in pipe does that, so we'll write our own.
Stock as a number Customers want words Write a pipe A pipe is a class, with the pipe decorator. The decorator gives it the name used in templates. The class has one method, transform. It receives the value, and returns what to show.
key value @Pipe({ name: '...' }) the name in templates class ... implements PipeTransform the pipe class transform(value) receives the value return what to show
The pipe decorator gives the pipe its name, stock label. Transform receives the value before the bar, and returns the text to show. Zero becomes sold out, and fewer than three becomes only a few left.
stock-label-pipe.ts typescript Copy
import { Pipe, PipeTransform } from '@angular/core' ;
@ Pipe ({ name: 'stockLabel' })
export class StockLabelPipe implements PipeTransform {
transform ( stock : number ) : string {
if (stock === 0 ) return 'Sold out' ;
if (stock < 3 ) return `Only ${ stock } left` ;
return 'In stock' ;
}
} We import the pipe class, like any other. Then each book's stock goes through stock label. Run it. Each book shows a label, instead of a number.
app.ts typescript Copy
import { Component } from '@angular/core' ;
import { StockLabelPipe } from './stock-label-pipe' ;
import { BOOKS } from './books' ;
@ Component ({
selector: 'app-root' ,
imports: [StockLabelPipe],
template: `
@for (b of books; track b.id) {
<p>{{ b.title }}: {{ b.stock | stockLabel }}</p>
}
` ,
})
export class App {
books = BOOKS ;
} Also uses stock-label-pipe.ts import { Pipe, PipeTransform } from '@angular/core' ;
@ Pipe ({ name: 'stockLabel' })
export class StockLabelPipe implements PipeTransform {
transform ( stock : number ) : string {
if (stock === 0 ) return 'Sold out' ;
if (stock < 3 ) return `Only ${ stock } left` ;
return 'In stock' ;
}
} Also uses books.ts export interface Book {
id : number ;
title : string ;
author : string ;
price : number ;
stock : number ;
}
export const BOOKS : Book [] = [
{ id: 1 , title: 'Dune' , author: 'Frank Herbert' , price: 9.99 , stock: 4 },
{ id: 2 , title: 'The Hobbit' , author: 'J.R.R. Tolkien' , price: 8.99 , stock: 0 },
{ id: 3 , title: 'Circe' , author: 'Madeline Miller' , price: 10.99 , stock: 2 },
{ id: 4 , title: 'Emma' , author: 'Jane Austen' , price: 6.99 , stock: 7 },
{ id: 5 , title: 'Persuasion' , author: 'Jane Austen' , price: 7.49 , stock: 1 },
]; Prefer to watch? The full Angular course is free All lessons in one video with animations, real code and chapters.
Watch free By default, a pipe is pure. Angular only runs it again when its input value changes. That makes pipes very cheap, even in long lists. So keep transform simple, with no side effects.
key value Pure the default Runs again only when the input changes Cheap even in long lists transform simple, no side effects
A custom pipe is a class with a name, and a transform method. Next, directives.
Key takeaways @Pipe({ name }) transform(value) Pure by default