Author -  Sai gowtham

Angular router query parameters (tutorial)

In this tutorial, we are going to learn about how to use query parameters in the angular router.

Query parameters

Query parameters are added to the end of URL with (?key=value) question mark followed by the key-value pairs by using that we can filter the data or we can use that data for other purposes.

Let’s see an example of how to use query parameters in routes.

app.component.html
 <nav>
    <a routerLink="/users" [queryParams]='{name:"sai"}'>User sai</a> </nav>

In the above code, we have added a new directive called queryParams to our users route.

queryParams : we need to pass our query parameters as an object to the queryParams directive.

Accessing query parameters data

To access the query parameters data inside UsersComponent we need to take the help of ActivatedRoute.

users.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
@Component({
  selector: 'app-userinfo',
  templateUrl: './userinfo.component.html',
  styleUrls: ['./userinfo.component.css']
})

export class UsersComponent implements OnInit {
  userName: string;  constructor(private route: ActivatedRoute) { }
  ngOnInit() {
     this.route.queryParams.subscribe(      (queryparams: Params) => {        this.userName = queryparams.name;      });  }
}

In the above code, we first imported ActivatedRoute service and Params type from the @angular/router package and we injected ActivatedRoute service to the component constructor so that we can use this service inside the component.

Inside ngOnInit method we are accessing the currently activated route queryParams data by subscribing it, and we are updating the userName property with currently loaded query params name.

queryparams.name where name property is the same we added to the [queryParams]='{name:"sai"}' directive.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY