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.

Share this:

  • Facebook
  • X

Related

Javascript Angular angularqueryparamurl

Post navigation

Previous post
Next post

Related Posts

Javascript Angular Dynamic Style CSS with Example

Angular Dynamic Style CSS with Example

October 5, 2022November 8, 2023

In this blog post, we’ll explore Angular dynamic style CSS with example in component’s styles based on certain conditions. Sometimes in our application we want dynamically change the color, size, font, background images so Angular provides various ways to conditionally apply styles to elements. In this post, we’ll explore three…

Share this:

  • Facebook
  • X
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…

Share this:

  • Facebook
  • X
Read More
Javascript How to use local storage in angular

How to use local storage in angular ?

April 18, 2022October 4, 2022

Local storage are used to store client side data in browser and in this tutorial we will learn to use local storage in angular. Angular is a javascript framework and used to enable to seamless developer experience to build web and mobile applications. Local storage are useful when we want…

Share this:

  • Facebook
  • X
Read More

Leave a ReplyCancel reply

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 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • 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

  • How to Call Controller Method from Another Controller in Laravel ?
  • How to Get Domain Name in Laravel ?
  • How to Append Query String to Route in Laravel ?
  • How to Append URL Query Params to Pagination Laravel ?
  • How to Get Today Records in Laravel ?
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version