myHotTake

What’s the Easiest Way to Programmatically Navigate in Angular?Angular Router navigation

If you find this analogy helpful, feel free to like or share


I’m a captain steering a ship through an ocean filled with islands. Each island represents a page or view in an Angular app. My ship? That’s the app’s current view. And my trusty map? That’s the Angular Router.

When I want to sail to a new island (or navigate to a new page), I don’t just randomly guess where it is. Instead, I use the Router’s navigate method. It’s like inputting the destination into a magical compass. I tell it, “Take me to the island named /dashboard or /profile,” and it sets the course instantly.

But it gets even cooler. Let’s say I have a crew member shouting, “Captain, take us to the ‘Orders’ page for User #42!” With Angular, I can pass extra coordinates—parameters! My navigation command looks something like this: this.router.navigate(['/orders', 42]). The Router not only takes me to the Orders island but also lets me know I need to focus on User #42 once I’m there.

And if I want to jump between sections of the same island, like moving from the north side (a tab) to the south side? I use query parameters—like sending a detailed note along with my navigation request.

The best part? The whole journey is smooth. No weird reloads, no hiccups—just seamless travel. That’s why I love Angular Router. It’s the ship’s compass that makes navigating my app’s pages as easy as a breeze.


Setting Sail with Router.navigate

If I want to navigate to a specific route like /dashboard, the code looks like this:

import { Router } from '@angular/router';

constructor(private router: Router) {}

goToDashboard() {
  this.router.navigate(['/dashboard']);
}

This command is like pointing the ship to the Dashboard island.


Passing Parameters: Delivering a Package 🚢📦

If my crew needs to get to the Orders page for User #42, I can send parameters:

goToUserOrders(userId: number) {
  this.router.navigate(['/orders', userId]);
}

Here, '/orders' is the route, and userId is the parameter. Angular’s Router ensures the right “package” (parameter) arrives at the right destination.


Adding More Details with Query Parameters ✍️

Suppose I’m sailing to an island, but I also need to leave instructions on what to do when I arrive. I can pass query parameters:

goToUserOrdersWithFilter(userId: number) {
  this.router.navigate(['/orders', userId], {
    queryParams: { filter: 'pending' },
  });
}

This adds a URL like /orders/42?filter=pending, ensuring the app knows to show only pending orders for User #42.


Optional Routes: Taking the Scenic Route 🗺️

Sometimes, parts of a route are optional. For example, if I might want to include a “section” on the island:

goToSection(section: string) {
  this.router.navigate(['/dashboard', { outlets: { sidebar: section } }]);
}

With this, my URL might look like /dashboard(sidebar:analytics), showing the analytics section in a sidebar.


Key Takeaways

  1. Router.navigate is your app’s captain’s wheel: it lets you programmatically move between routes.
  2. Route parameters pass specific data to a route, like userId.
  3. Query parameters add extra context or instructions, like filters or sorting.
  4. Optional or auxiliary routes provide flexibility for complex navigation.