Router Events and Navigation
Respond to navigation lifecycle events.
Router Events and Navigation is a free Angular Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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/navigateByUrlorrouterLink.
You have completed Angular Router Advanced.
Frequently asked questions
Is the “Router Events and Navigation” lesson free?
Yes — the full text of “Router Events and Navigation” is free to read here on the web, and the Angular Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Angular Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
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
- Route Parameters and Query Params
- Resolvers and Route Data
- Functional Route Guards
- Router Events and Navigation