Skip to content
Related Articles

Related Articles

How to pass query parameters with a routerLink ?

View Discussion
Improve Article
Save Article
  • Last Updated : 22 Jul, 2020

The task is to pass query parameters with a routerLink, for that we can use the property binding concept to reach the goal. Using property binding, we can bind queryParams property and can provide the required details in the object.

What is Property Binding?

It is a concept where we use square bracket notation to bind data to Document Object Model(DOM) properties of Hypertext markup language(HTML) elements.


<a [routerLink]="[/path]" [queryParams]="{property:value}">
   State Details 

An Example of property binding:


import { Component, OnInit } from '@angular/core'
    selector: 'app-property',
        // Property Binding 
        `<p [textContent]="title"></p>`
export class AppComponent implements OnInit {
    constructor() { }
    ngOnInit() { }
    title = 'Property Binding example in GeeksforGeeks';


Illustration of above code


  • First, configure the routes in app.module.ts
  • Implement query params with required values in the HTML file.
  • Then in .ts file, in ngOnit try to access the query params using the activated Route by importing it  from ‘angular@router’
  • Once you are able to access them try to render them by using either string interpolation syntax or property binding syntax in HTML file.

Below is the implementation of the above steps:



import { NgModule } from '@angular/core';
import { BrowserModule } from 
// Importing Routes
import { RouterModule, Routes } 
        from '@angular/router';
import { AppComponent } from 
import { StateComponent } from 
// Configuring Routes
const routes: Routes = [{ 
    path: 'punjab'
    component: StateComponent 
    imports: [BrowserModule, R
    declarations: [AppComponent, 
    bootstrap: [AppComponent]
export class AppModule { }



<a [routerLink]="['/punjab']" [queryParams]=
    State Details

After clicking on the anchor tag the URL will be displayed in the following way:

We can also access the query parameters using the activated route.

In this way, we can pass query parameters via routerLink.

Fetching of query parameters:

state.component.ts :


import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
  selector: 'app-state',
  templateUrl: './state.component.html',
  styleUrls: ['./state.component.css']
export class StateComponent implements OnInit {
  capital: string;
  constructor(private route: ActivatedRoute) { }
  ngOnInit() {
      params => { =  params['capital'];



State Details are : 
<p>Capital  : {{capital}} </p>
<p>Language : {{language}} </p>


  • Before Click the Button:

  • After Click the Button: Hence query parameters passed can be seen

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!