Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to get query string params in angular 12

How to get query string params in angular 12 ?

Aman Jain, April 6, 2022October 4, 2022

In this tutorial i will show you to get the query string params in angular using ActivatedRoute. Angular completely works in different way from other javascript frameworks, Angular uses its own routing framework to land between the pages. Let’s quickly deep dive in to the topic and learn it.

So before start we should have knowledge of angular routing modules, i am assuming that you know about angular routing.

Suppose we have this URL

https://domain.com/post?id=1

then Simplest way to get the query param

import { Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-helloworld',
  templateUrl: './helloworld.component.html',
  styleUrls: ['./helloworld.component.css']
})
export class HelloworldComponent {
  constructor(public activatedRoute: ActivatedRoute) {
    //this will only give first time and will not subscribe if there is change in url
    let id=this.activatedRoute.snapshot.queryParamMap.get("id"));
    //or 
   //it will subscribe the url changes and will update the component accordingly
    this.activatedRoute.queryParamMap.subscribe(data=>{
       alert(data.get("id"));
    });
   }
}

As you can see we have used here ActivatedRoute from '@angular/router' and then injected in constructor.

here we used two methods one is using snapshot and other is using subscribe, snapshot not listen for changes in url but subscribe do listen for changes and we can get query parameter using get method.

I will show you the difference between snapshot vs subscribe in activated route. let’s begin the tutorial of get query string params in angular step by step

Step 1 : Create an angular app

First step is to setup an angular app using below command

ng new example-app

Now, this will ask for

Would you like to add Angular routing?, select Y

Step 2: Create a Component

Create a component so we can add conditional statements

ng g c hello-world
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-hello-world',
  templateUrl: './hello-world.component.html',
  styleUrls: ['./hello-world.component.css'],
})
export class HelloWorldComponent implements OnInit {
  id: any;
  subscribed_id: any;
  constructor(public activatedRoute: ActivatedRoute) {
    this.id = this.activatedRoute.snapshot.queryParamMap.get('id');

    this.activatedRoute.queryParamMap.subscribe((data) => {
      this.subscribed_id = data.get('id');
    });
  }

  ngOnInit() {}
}

as we described above subscribe and snapshot, if you check stackblitz example below then you will find the difference easily since one is subscribing the changes and other not on changing the url.

Step 3 : Create template and add html

Now, add simple html template with a link and show id

<h1>POST PAGE</h1>
<a routerLink="/post" [queryParams]="{ id: '4' }">Go To Post 4</a> <br />
Current URL ID IS. {{ id }}
<br>
Current SUBSCRIBED ID IS. {{ subscribed_id }}

Live Example and code:

Here if you click on go to post 1 and then again go to post 4 then you will be able to understand that subscribe method is changing continuously.

Related

Javascript Angular angularqueryparamurl

Post navigation

Previous post
Next post

Related Posts

Javascript How to set and get cookies in angular

How to set and get cookies in angular ?

April 14, 2022November 6, 2023

This article will give you the understanding of set and get cookies in angular. Cookies are used to store the data in client computer, Cookie can hold tiny information in computer and can retrieve when required for same website. Cookies can store key value in client system and its send…

Read More
Javascript reusable components in angular

How to make reusable components in angular?

September 11, 2021October 4, 2022

Another rich feature of angular is reusability of component, Angular components are those components which we can reuse multiple times in a module or application. Angular reusable component can be dynamic and much flexible to send updates on any event . Before start, I assume you well aware of installation…

Read More

How to check jQuery checkbox checked or not ?

January 25, 2022January 25, 2022

In this article i will show you multiple way to check or validate if a checkbox is checked or not. Also i will show you to check a checkbox programmatically. In the post we will learn all possibilities to validate a checkbox and how we can check a checkbox using…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version