Angular Route Parameters: Passing Parameters to Route

In this tutorial, we learn how to pass parameters (route parameter) or data to the Route in Angular. First, let us look at how to define the route, which accepts the parameter.  We then learn how to pass the parameters to the route using the routerLink directive. Finally, we learn how to retrieve the parameters using the ActivatedRoute Service.  The parameters can be retrieved by either using snapshot method or by subscribe method. We take a look at both of these methods.

There are many scenarios, where you need to pass parameters to the route. For example, to navigate to the product detail view, we need to pass the product ID, so that component can retrieve it and display it to the user.

Let us build on the Application we built in the Routing & Navigation tutorial and add a product detail page, which displays the product detail based on the Product ID passed in the URL. The code for this tutorial available on Github under the folder parameters.

What are Route Parameters

The Route parameters are a dynamic part of the Route and essential in determining the route.

For example, consider the following route

The above route match only if the URL is /product

To retrieve the product details, our URL should look something like

/product/1
/product/2

Where the second URL segment ( 1 and 2 ) being the id of the product. The id is dynamic and changes as per the selected Product. To handle such scenario angular router allows us to include route parameters, where we can send any dynamic value for a URL segment

How to Pass parameters to Angular Route

Defining the Route

We can define parameter by adding forward slash followed colon and a placeholder (id) as shown below

Now above path matches the URLs /product/1 , /product/2, etc.

If you have more than one parameter, then you can extend it by adding one more forward slash followed colon and a placeholder

The name id, id1 & id2 are placeholders for parameters. We will use them while retrieving the values of the parameters.


Best Angular Books
The Top 8 Best Angular Books, which helps you to get started with Angular  

Defining the Navigation

We, now need to provide both path and the route parameter routerLink directive.

This is done by adding the productID as the second element to the routerLink parameters array as shown below

Which translates to the URL /product/2

OR

Which, dynamically takes the value of id from the product object.

Retrieve the parameter in the component

Finally, our component needs to extract the route parameter from the URL

This is done via the ActivatedRoute service from angular/router module to get the parameter value

ActviatedRoute

The ActivatedRoute is a service, which keeps track of the currently activated route associated with the loaded Component.

To use ActivatedRoute, we need to import it in our component

Then inject it into the component using dependency injection

ParamMap

The Angular adds the map all the route parameters in the ParamMap object, which can be accessed from the ActivatedRoute service

The ParamMap makes it easier to work with parameters. We can use get or getAll methods to retrieve the value of the parameters in the component. Use the has method to check if a certain parameter exists.

The Older version of ActivatedRoute class has a Params array which is an array of the parameter values, indexed by name. You can still use it but It is now deprecated and is replaced by the ParamMap.

There are two ways in which you can use the ActivatedRoute to get the parameter value from the ParamMap object.

  1. Using Snapshot
  2. Using observable

Using Snapshot

The snapshot property returns the initial value of the route. You can then access the paramsMap array, to access the value of the id, as shown above,

Use the Snapshot option, if you only need the initial value.

Using Observable

You can retrieve the value of id by subscribing to the paramMap observable property of the activateRoute as shown above

Use this option if you expect the value of the parameter to change over time.

Why use observable

We usually retrieve the value of the parameter in the ngOninit life cycle hook, when the component initialised.

When the user navigates to the component again, the Angular does not create the new component but reuses the existing instance. In such circumstances, the ngOnInit method of the component is not called again. Hence you need a way to get the value of the parameter.

By subscribing to the observable paramMap property, you will retrieve the latest value of the parameter and update the component accordingly.

The above difference is explained in our next tutorial Angular child routes tutorial.

Passing Parameters to Route: Example

Here is the complete list of code.

product.component.ts

product.component.html

product.service.ts

product.ts

In the product.component.html, we have added product.productID as the second argument to the routerLink parameters array.

Product Details Component

product-detail.component.ts

product-detail.component.html

In the ProductDetailComponent, we have imported router and ActivatedRoute from the angular router module

In the constructor, we inject the ActivatedRoute, Router service along with ProductService

Finally, we use ngOninit life cycle hook to retrieve the value of the id parameter and use that value to retrieve the details of the product.

Note that, there are two ways, by which you can retrieve the data.

Using snapshot

Using Subscribe

We used snapshot method to retrieve the parameter in the ProductDetailcomponet.ts. To Subscribe to params remove the ngOnInit and replace it with the following code

We recommend you to use the subscribe method as it offers the benefit of responding to the parameter changes dynamically.

The Routes

app.routing.ts

We have added the following route to our routes array

Other components

app.component.ts

app.component.html

contact.component.ts

home.component.ts

error.component.ts

app.module.ts

Passing data to Routes using Angular Route Parameters

ActivatedRoute

The ActivatedRoute service has a great deal of useful information including:

url: This property returns an array of Url Segment objects, each of which describes a single segment in the URL that matched the current route.

params: This property returns a Params object, which describes the URL parameters, indexed by name.

queryParams: This property returns a Params object, which describes the URL query parameters, indexed by name.

fragment: This property returns a string containing the URL fragment.

Snapshot: The initial snapshot of this route

data: An Observable that contains the data object provided for the route

Component: The component of the route. It’s a constant

outlet: The name of the RouterOutlet used to render the route. For an unnamed outlet, the outlet name is primary.

routeConfig: The route configuration used for the route that contains the origin path.

parent: an ActivatedRoute that contains the information from the parent route when using child routes.

firstChild: contains the first ActivatedRoute in the list of child routes.

children: contains all the child routes activated under the current route

pathFromRoot: The path from the root of the router state tree to this route

Conclusion

We looked at how to pass parameters or data to the Route. The parameters are passed to the route by using routerLink parameters in the routerLink directive. We retrieve the parameters from ActivatedRoute Service by reading params collection of the snapshot object or by subscribing to the params observable.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top