myHotTake

What’s the Difference Between NavigationStart & NavigationEnd?

If you find this analogy helpful, give it a like or share it with someone who loves learning with stories!


Think of navigating a website like planning a road trip. Imagine I’m heading to a friend’s house across town. The moment I decide to leave and step out my door, that’s NavigationStart. It’s the event that signals I’m done thinking about going and have actually begun the journey. The car engine starts, the GPS gets set, and I hit the road.

Now, as I drive, there’s a bit of suspense. Will I hit traffic? Will I need to take a detour? These moments don’t count as their own special events in this analogy—they’re just part of the trip. I’m focused on the destination.

Finally, I pull into my friend’s driveway, step out of the car, and ring the doorbell. That’s NavigationEnd. It means the journey’s over, I’ve arrived safely, and I can settle into the next phase of my visit: hanging out with my friend.

So, NavigationStart is like revving the engine and hitting the road, while NavigationEnd is the satisfying moment I’ve reached my destination. Everything in between? That’s just the journey playing out.


In our analogy, NavigationStart is like starting your trip, and NavigationEnd is arriving at the destination. Let’s see how these events look in Angular’s Router, which makes handling navigation events straightforward.

Here’s a simple example:

import { Router, NavigationStart, NavigationEnd } from '@angular/router';
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `<div *ngIf="loading" class="loading-spinner"></div>`,
})
export class AppComponent {
  loading = false;

  constructor(private router: Router) {
    // Subscribe to router events
    this.router.events.subscribe((event) => {
      if (event instanceof NavigationStart) {
        // Trip starts: Show loading spinner
        this.loading = true;
        console.log('Navigation started:', event.url);
      } else if (event instanceof NavigationEnd) {
        // Trip ends: Hide loading spinner
        this.loading = false;
        console.log('Navigation ended:', event.url);
      }
    });
  }
}

Code Breakdown

  1. NavigationStart:
    • This event is triggered when a navigation starts.
    • In our analogy, this is the point when you leave your house.
    • The code sets loading to true and logs the start of the trip.
  2. NavigationEnd:
    • This event fires when the navigation is successfully completed.
    • Analogous to arriving at the destination.
    • The code sets loading to false and logs the successful arrival.
  3. Other Router Events:
    • While NavigationStart and NavigationEnd are commonly used, Angular has more events like NavigationCancel (if the trip is abandoned) and NavigationError (if there’s a crash along the way). These can also be handled similarly.

Key Takeaways/Final Thoughts

  • NavigationStart and NavigationEnd are crucial lifecycle hooks for managing UI behavior during route changes, like showing a loading spinner or logging.
  • Angular’s Router provides a robust way to listen for these events with the events observable.
  • This approach is especially useful for improving user experience, such as providing feedback when pages are loading.

Understanding these events is like mastering the rhythm of a journey—knowing when the trip starts, ends, or even when something goes wrong. And once you know that rhythm, you can make every navigation smoother and more delightful for users.