Angular Pass Data to Route: Dynamic/Static

Angular allows us to pass data through the route. The route data can be either static or dynamic. The static data use the Angular route data property, where you can store arbitrary data associated with that specific route. To pass dynamic data (or an object), we can use the history state object. The Routed Component can then retrieve the dynamic data from the history state object.

Various ways of passing route data

Angular components can share data in many ways. The parent can communicate with their child using @Input directive. A child can pass data to the Parent using the @Output & EventEmitter. The parent can use the @ViewChild to access the child component. In case of components are unrelated then we can use the Angular Services to Share data between them.

We can also share data between components using the route. Angular can pass data through the route in several ways.

  1. Using Route Parameter
  2. The Query Parameters or Query Strings
  3. Using URL Fragment
  4. Static data using the data property
  5. Dynamic data using the state object

In this article, we look at how to pass static or dynamic data using the Route. i.e. items 4 & 5 in the above list.

Passing static data to a route

We can configure the static data at the time of defining the route. This is done by using the Angular route data property of the route. The route data property can contain an array of arbitrary string key-value pairs. You can use the static data to store items such as page titles, breadcrumb text, and other read-only, static data

For Example, consider the following route with the data property set

The Angular Router will pass the { id:'1', name:"Angular"} when the StaticComponent is rendered. The data value will be located in the data property of the ActivatedRoute service

We can then read the data by subscribing to the property as shown below

Passing Dynamic data to a Route

The option to pass the dynamic data or a user-defined object was added in Angular Version 7.2 using the state object. The state object is stored in History API

Providing the State value

The state can be provided in two ways

Using routerLink directive

Using navigateByUrl


navigationId is a number, which is incremented every time we navigate from one route to another. Angular uses it to identify every navigation. The Router will add a navigationId property to the state object. Because of that, we can only assign an object to the State object.

Hence we cannot store primitive types like strings or numbers etc. For example, the following code results in an error because we are passing a string.

You can only assign an object to the state object. The following code is ok.

Accessing the state value

The state can be accessed by using the getCurrentNavigation method of the router (works only in the constructor)

Or use the history.state in the ngOnInit.

or use the getState method of the Location Service. This method is available in Angular 8+

Note that you will lose the dynamic data if the page is refreshed.

Passing Data to the Routes Example

Let us build a simple project to demonstrate how to pass data to the route

Passing static data example


Source Code

The static component gets the static data configured in the route. It subscribes the property to get the product data as shown above.

Passing dynamic data (or object) example


Source Code

The Dynamic Component gets dynamic data. We use the history.state to access the product data. Alternatively, we can use the this.router.getCurrentNavigation().extras.state to achieve the same. Please remember getCurrentNavigation only works in the constructor. It will return null if used elsewhere.


Source Code

In HomeComponent, we have used routerLink & navigateByUrl to pass the data to the dynamic component. You can also use the form fields to change the data, before passing it to the dynamic route.


Source Code

Here the static data is set for StaticComponent using the data property.


Source Code


Source Code

Angular pass data to route


History API

14 thoughts on “Angular Pass Data to Route: Dynamic/Static”

  1. { navigationId : 12 ,
    search : “data” }

    i got this in using of history.state method for read data
    so my question is what is “navigationId”

    1. navigationId is a number, which is incremented every time we navigate from one route to another. Angular uses it to identify every navigation. The Router will add a navigationId property to the state object

      Updated the article

  2. Excellent!, simple and quickly just someone needed, just remeber everytime you subscribe in a component always unsubscribe on ngOnDestroy cycle event of that component.

  3. routing overwrites passed queryParams described in url and redirects it to the same page without by cutting queryParams . how is possible to configure routing to accept queryparams and keep it during navigation?

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top