Creating Pipes page

Learn how to create a custom pipe in Angular that returns a modified version of a string.


You may have noticed an image error in our rendered html page. We're using an API in this demo that wasn't built for our exact purposes, and we need a different image path for our app to serve.

In this exercise, we will fix the path of the thumbnail images in src/app/restaurant/restaurant.component.html.

Currently the path is written out like:

<img src="{{ restaurant.images.thumbnail }}" width="100" height="100" />

restaurant.images.thumbnail will be a path like node_modules/place-my-order-assets/image.png. We need to change that path to be more like ./assets/image.png. Once the path rewriting is fixed, images will show up correctly.

What You Need to Know

  • How to generate a pipe
  • How to use a pipe to transform data

How to Generate a Pipe via the CLI

Generate a pipe with the following command:

ng g pipe imageUrl

This will generate a pipe file: image-url.pipe.ts

How to Build a Pipe

Angular Pipes come in handy to transform content in our templates. Pipes allow us to transform data to display to the user in our HTML without modifying the original source.

Angular comes with several built-it pipes like DatePipe, UpperCasePipe, LowerCasePipe, CurrencyPipe, and PercentPipe. These pipes can be used in templates to modify the way data displays. We can build custom pipes as well. Pipes require one parameter - the value we want to change, but can take an additional parameters as well.

This example takes the value to be transformed and a parameter to use as an exponential multiplier.

<script src=""></script>
<script src=""/></script>
<script src=""/></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>
<script type="typescript">
// app.js
const { Component, VERSION, Pipe, PipeTransform } = ng.core;

@Pipe({name: 'exponentialStrength'})
class ExponentialStrengthPipe implements PipeTransform {
  transform(value: number, exponent: number): number {
    return Math.pow(value, isNaN(exponent) ? 1 : exponent);

  selector: 'my-app',
  template: `
  <h2>Power Booster</h2>
  <p>Super power boost: {{2 | exponentialStrength: 10}}</p>
class AppComponent {
  constructor() {

// main.js
const { BrowserModule } = ng.platformBrowser;
const { NgModule } = ng.core;
const { CommonModule } = ng.common;

  imports: [ BrowserModule,
  declarations: [AppComponent, ExponentialStrengthPipe],
  bootstrap: [AppComponent],
  providers: []
class AppModule {}

const { platformBrowserDynamic } = ng.platformBrowserDynamic;

  .catch(err => console.error(err));

Technical Requirements

  1. Use an imageUrl pipe in src/app/restaurant/restaurant.component.html to rewrite the path. Using a pipe looks like the following:
<img src="{{ restaurant.images.thumbnail | imageUrl }}" />
  1. Generate and implement the imageUrl pipe.

The pipe will take an image url and transform it to the path we actually want to serve the image from. For example, from node_modules/place-my-order-assets to ./assets. This pipe will be used on our restaurant image thumbnail.

Hint: Use String.prototype.replace to create the new path with image name.


✏️ Update src/app/restaurant/restaurant.component.html file to use the pipe we will create:

<div class="restaurants">
  <h2 class="page-header">Restaurants</h2>
  <ng-container *ngIf="restaurants.length">
    <div class="restaurant" *ngFor="let restaurant of restaurants">
        src="{{ restaurant.images.thumbnail | imageUrl }}"
      <h3>{{ }}</h3>

      <div class="address" *ngIf="restaurant.address">
        {{ restaurant.address.street }}<br />{{ }},
        {{ restaurant.address.state }} {{ }}

      <div class="hours-price">
        $$$<br />
        Hours: M-F 10am-11pm
        <span class="open-now">Open Now</span>

      <a class="btn" [routerLink]="['/restaurants', restaurant.slug]">
      <br />

✏️ Run the following to generate the pipe and the pipe's tests:

ng g pipe imageUrl

How to Verify Your Solution is Correct

✏️ Update the restaurant spec file src/app/restaurant/restaurant.component.spec.ts to include the new pipe:

import {
} from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { ImageUrlPipe } from '../image-url.pipe';
import { RestaurantComponent } from './restaurant.component';

describe('RestaurantComponent', () => {
  let fixture: ComponentFixture<RestaurantComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [RouterTestingModule],
      declarations: [RestaurantComponent, ImageUrlPipe],

    fixture = TestBed.createComponent(RestaurantComponent);

  it('should create', () => {
    const component: RestaurantComponent = fixture.componentInstance;

  it('should render title in a h2 tag', () => {
    const compiled = fixture.nativeElement as HTMLElement;

  it('should not show any restaurants markup if no restaurants', () => {
    const compiled = fixture.nativeElement as HTMLElement;

  it('should have two .restaurant divs', fakeAsync((): void => {
    const compiled = fixture.nativeElement as HTMLElement;
    const restaurantDivs = compiled.getElementsByClassName('restaurant');
    const hoursDivs = compiled.getElementsByClassName('hours-price');

  it('should display restaurant information', fakeAsync((): void => {
    const compiled = fixture.nativeElement as HTMLElement;
    expect(compiled.querySelector('.restaurant h3')?.textContent).toContain(
      'Poutine Palace'

✏️ Update the spec file src/app/image-url.pipe.spec.ts to be:

import { ImageUrlPipe } from './image-url.pipe';

describe('ImageUrlPipe', () => {
  it('create an instance', () => {
    const pipe = new ImageUrlPipe();

  it('returns a string with proper assets path', () => {
    const pipe = new ImageUrlPipe();

If you've implemented the solution correctly, when you run npm run test all tests will pass!


Click to see the solution ✏️ Update src/app/image-url.pipe.ts to:

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

  name: 'imageUrl',
export class ImageUrlPipe implements PipeTransform {
  transform(value: string): string {
    return value.replace('node_modules/place-my-order-assets', './assets');