Angular Academy · Lesson

Router Events and Navigation

Respond to navigation lifecycle events.

Lesson 4 of 413 steps

Router Events and Navigation is a free Angular Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, and your progress syncs across the web and the CoddyKit app. The Angular Academy course includes 4 lessons in total.

The router event stream

Router.events is an observable that emits during the navigation lifecycle: start, recognition, guard checks, resolve, and end. Subscribe to react to navigation globally.

Key event types

Common events: NavigationStart, NavigationEnd, NavigationCancel, NavigationError. Each is a class instance you can filter for.

Listening for NavigationEnd

Filter the stream to a specific event type with filter and an instanceof check.

import { Router, NavigationEnd } from '@angular/router';
import { filter } from 'rxjs';

const router = inject(Router);
router.events.pipe(
  filter(e => e instanceof NavigationEnd)
).subscribe(e => console.log('navigated to', e.urlAfterRedirects));

Global loading indicator

Show a spinner between NavigationStart and NavigationEnd/Cancel/Error.

router.events.subscribe(e => {
  if (e instanceof NavigationStart) this.loading = true;
  if (e instanceof NavigationEnd ||
      e instanceof NavigationCancel ||
      e instanceof NavigationError) this.loading = false;
});

Scroll to top on navigation

Reset scroll position after each successful navigation. (Built-in scrollPositionRestoration also exists.)

router.events.pipe(
  filter(e => e instanceof NavigationEnd)
).subscribe(() => window.scrollTo(0, 0));

Analytics page tracking

NavigationEnd is the ideal hook to log page views to analytics.

router.events.pipe(
  filter((e): e is NavigationEnd => e instanceof NavigationEnd)
).subscribe(e => analytics.pageView(e.urlAfterRedirects));

Programmatic navigation

router.navigate takes a commands array; router.navigateByUrl takes a full URL string. Both return a promise resolving to whether navigation succeeded.

router.navigate(['/user', 42]);
router.navigateByUrl('/search?q=ng');

const ok = await router.navigate(['/home']);
console.log('success?', ok);

Relative navigation

Use relativeTo to navigate relative to the current route, e.g. from a child component.

const route = inject(ActivatedRoute);
router.navigate(['../sibling'], { relativeTo: route });

routerLink and active state

In templates, routerLink handles navigation declaratively, and routerLinkActive adds a CSS class to the active link.

<a routerLink="/home" routerLinkActive="active">Home</a>
<a [routerLink]="['/user', userId]"
   routerLinkActive="active">Profile</a>

NavigationError handling

Subscribe to NavigationError to handle failed navigations (e.g. a failed resolver or lazy-load) and show a friendly message.

router.events.pipe(
  filter(e => e instanceof NavigationError)
).subscribe(e => console.error('nav failed', e.error));

Typed event filtering

Angular also offers RouterEvent typing; a type-guard predicate in filter gives you a correctly typed event in the subscriber without casting.

Quick Check

Test your understanding of router events.

Recap: Router Events & Navigation

Router.events exposes the navigation lifecycle.

  • Filter for NavigationStart/End/Cancel/Error.
  • Drive loading spinners, scroll reset, and analytics.
  • Navigate via navigate/navigateByUrl or routerLink.

You have completed Angular Router Advanced.

Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
38
Lessons
144

Frequently Asked Questions

Is the “Router Events and Navigation” lesson free?

Yes — any 3 lessons of the Angular Academy learning path, including “Router Events and Navigation”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Angular Academy course includes 4 lessons in total.

What will I learn in “Router Events and Navigation”?

Respond to navigation lifecycle events. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Angular Academy?

No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Router Events and Navigation” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Angular Academy lesson?

Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Route Parameters and Query Params
  2. Resolvers and Route Data
  3. Functional Route Guards
  4. Router Events and Navigation
← Back to Angular Academy