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

Softwares disable change detection anagular

How to disable automatic change detection strategy in angular component?

July 4, 2021November 8, 2023

In angular all components by default take the changes as any event occur, but sometime we disable automatic change detection strategy in angular. Thus, to detach the two way binding in component we can use changeDetection strategy in component metadata. Angular Change Detection Strategy is simple to implement using the…

Read More
Javascript Laravel Ajax Autocomplete Using Select2

Laravel Ajax Autocomplete Using Select2

July 17, 2022July 17, 2022

In this article we will learn to use Laravel Ajax Autocomplete Using Select2. Select2 is useful when we want live search of bulk data or to convert the existing select boz with multi features like search, multi select and options customizations. Autocomplete search is mostly work of javascript and when…

Read More
Softwares angular file upload

How to upload multiple files in angular 12 reactive form

June 20, 2021November 8, 2023

How to upload multiple files in angular 12 reactive form Uploading multiple files with progress in angular is too easy, you can implement it in your website using the below guideline. i will suggest to read my last post Angular file upload with post form data and php $_FILES to…

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

  • 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

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version