Passing Parameters to Routes

Learn how to pass parameters to routes when paths change dynamically.

We learned that we can navigate to a route with a static path value. In this lesson, we will learn how to do this when the path changes dynamically using route parameters.

A common scenario in enterprise web applications is to have a list of items and when we click on one of them, the page changes the current view and displays details of the selected item. The previous approach resembles a master-detail browsing functionality, where each generated URL on the master page contains the identifiers required to load each item on the detail page.

We can represent the previous scenario with two routes navigating to different components. One component is the list of items, and the other is the details of an item. So, we need to find a way to create and pass dynamic item-specific data from one route to the other.

We are tackling double trouble here: creating URLs with dynamic parameters at runtime and parsing the value of these parameters. No problem: the Angular router has our back, and we will see how with a real example.

Building a detail page using route parameters

The product list in our application currently displays a list of products. When we click a product, the product details appear below the list. We need to refactor the previous workflow so that the component responsible for displaying product details is rendered on a different page from the list. We will use the Angular router to redirect the user to the new page upon clicking a product from the list.

Currently, the product list component passes the id of the selected product via input binding. Input binding cannot be used if the component is activated with routing. We will use the Angular router to pass the product id as a route parameter:

  1. Open the products-routing.module.ts file and add a new route definition:

Get hands-on with 1200+ tech skills courses.