Soufian Chaoui
Soufian Chaoui

Reputation: 47

Get URL parameters in Angular

I'm trying the get the url parameters in angular i've tried

 ngOnInit() {   

   this.route.paramMap.subscribe(params => {
     console.log("test1",params)
}) 
//and
console.log("test2", this.route.snapshot.params)

 }
ngAfterViewInit(){


}

The response it get : enter image description here

Upvotes: 0

Views: 432

Answers (2)

Kheder
Kheder

Reputation: 203

Are you using ActivatedRouter class or Router class? When router is of class Router then this should work.

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

...
constructor(private router: Router) {
     this.router.params.subscribe(params => {
     console.log('my parameters: ', params);
    });
}

Upvotes: 0

Zee
Zee

Reputation: 483

constructor() {
    const query = new URLSearchParams(location.search);
    console.log(query.get('param_key')); // www.google.com?size=123 --- param_key is size here

}

Try out this and let me know if missing anything ... Happy coding ... :)

Upvotes: 1

Related Questions