ItSolutionStuff.com

How to Get Query String from URL in Angular?

By Hardik Savani • May 2, 2024
Angular

Are you looking for angular get query string parameter from url? if yes then i will help you to how to get query string params from current url in angular 8 component application. we will get query string from url using ActivatedRoute in angular app. You can get easily in angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13, angular 14, angular 15, angular 16 and angular 17.

Many times we need to get query string parameters in angular app. You can easily get query string using ActivatedRoute. i will show you more examples for how to get query string value in angular 8 application.

Let's see bellow example, that will help you:

Get All Query String Parameters:

You can get query string in your component like as bellow:

component

import { Component, OnInit } from '@angular/core';

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

@Component({

selector: 'app-posts',

templateUrl: './posts.component.html',

styleUrls: ['./posts.component.css']

})

export class PostsComponent implements OnInit {

constructor(private router: ActivatedRoute) { }

ngOnInit() {

this.router.queryParams.subscribe(params => {

console.log(params);

});

}

}

URL:

http://localhost:4200/posts?id=12&name=Hardik

Output:

{id: "12", name: "Hardik"}

Get One Query String Param Value:

You can get query string in your component like as bellow:

component

import { Component, OnInit } from '@angular/core';

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

@Component({

selector: 'app-posts',

templateUrl: './posts.component.html',

styleUrls: ['./posts.component.css']

})

export class PostsComponent implements OnInit {

constructor(private router: ActivatedRoute) { }

ngOnInit() {

console.log(this.router.snapshot.queryParamMap.get('id'));

}

}

URL:

http://localhost:4200/posts?id=12&name=Hardik

Output:

12

Now you can use in your app.

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Angular Show Error Message on Submit Example

Read Now →

How to Disable Enter Key to Submit Form in Angular?

Read Now →

Angular Json Pipe Example | Json Pipe in Angular

Read Now →

Angular NgIf Else | Ng If Else in Angular Example

Read Now →

Angular Material Input Box Example

Read Now →

How to Allow Only Numbers in Textbox in Angular?

Read Now →

Angular 9/8 HttpClient for Sending Http Request Example

Read Now →

Angular Get Current Route Name Example

Read Now →

Angular 9/8 Routing and Nested Routing Tutorial With Example

Read Now →

Angular Change Date Format in Component Example

Read Now →

How to Create New Component in Angular 8?

Read Now →

Angular NgClass - How to Add Dynamic Class in Angular 10/9/8?

Read Now →

Reactive Form with Validation in Angular 8

Read Now →

How to Add Bootstrap in Angular 8 | Install Bootstrap 4 in Angular 8

Read Now →