← Back to Angular, page 6

angular-0111 · Easy fresher

How do you show a loading indicator during navigation using router events?

The Router publishes events such as NavigationStart, NavigationEnd, NavigationCancel and NavigationError. Listen to router.events, set a loading signal to true on start and false on any of the three end events, and show the indicator while it is true.

Full answer

Lazy loaded routes, guards and resolvers take time. Users should see that something is happening.

The Router has an events observable. The main events for one navigation are:

  1. NavigationStart: navigation begins.
  2. RoutesRecognized, GuardsCheckStart, GuardsCheckEnd, ResolveStart, ResolveEnd: the stages in between.
  3. One of three endings: NavigationEnd (success), NavigationCancel (a guard returned false or a newer navigation replaced it) or NavigationError (an error, such as a chunk that failed to load).

To show a spinner:

  • Filter the events to the four kinds, and map them to a boolean: true for start, false for the three endings.
  • Convert it to a signal with toSignal(..., { initialValue: false }).
  • In the template, @if (loading()) { <progress /> }.

Why handle all three endings? If you only hide on NavigationEnd, a cancelled or failed navigation leaves the spinner forever.

Variations:

  • Add a small delay before showing the indicator (about 150 ms), so fast navigations do not flash.
  • Use the withNavigationErrorHandler router feature to redirect to an error page.
  • For a global progress bar, put this logic in one root component, not in each page.
  • Add aria-busy on the main region and an accessible label on the indicator.

Remember that router.events only emits while someone subscribes, so use it in a component or service created at start-up. In SSR, events also fire on the server.

typescript
import { Component, inject } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { NavigationCancel, NavigationEnd, NavigationError, NavigationStart, Router, RouterOutlet } from '@angular/router';
import { filter, map } from 'rxjs';

@Component({
  selector: 'app-shell',
  imports: [RouterOutlet],
  template: `
    @if (loading()) {
      <progress aria-label="Loading page"></progress>
    }
    <main [attr.aria-busy]="loading()"><router-outlet /></main>
  `,
})
export class ShellComponent {
  private readonly router = inject(Router);

  readonly loading = toSignal(
    this.router.events.pipe(
      filter(
        (e) => e instanceof NavigationStart || e instanceof NavigationEnd || e instanceof NavigationCancel || e instanceof NavigationError,
      ),
      map((e) => e instanceof NavigationStart),
    ),
    { initialValue: false },
  );
}

Angular 20.3, strict template check passes. A Karma test checks that the signal is true during a navigation and false after it ends.

Key points

  • `router.events` emits navigation events. Start begins, and End, Cancel or Error finish.
  • Handle all three ending events or the spinner can stay on forever.
  • `toSignal` gives a simple signal for the template.
  • Delay showing the indicator to avoid flashes. Add accessible labels.

Common mistakes

  • Only handling `NavigationEnd`, so cancelled navigations leave the spinner on.
  • Subscribing in every page component instead of once at the root.
  • Forgetting to unsubscribe in older code that subscribes manually.

Follow-ups

  • When is `NavigationCancel` emitted?
  • How would you delay showing the spinner by 150 ms?
  • How would you handle a lazy chunk that fails to load?

Tips

  • List the three endings. It is the usual trap.
  • Use `toSignal` to show modern style.
  • Mention accessibility.

Evaluation rubric

  • Names the navigation events correctly.
  • Handles all ending events.
  • Implements the indicator cleanly.