Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.
By injecting an instance of ActivatedRoute
one can subscribe to a variety of observables, including a queryParams
and a params
observable:
import {Router, ActivatedRoute, Params} from '@angular/router';
import {OnInit, Component} from '@angular/core';
@Component({...})
export class MyComponent implements OnInit {
constructor(private activatedRoute: ActivatedRoute) {}
ngOnInit() {
// Note: Below 'queryParams' can be replaced with 'params' depending on your requirements
this.activatedRoute.queryParams.subscribe(params => {
const userId = params['userId'];
console.log(userId);
});
}
}
A NOTE REGARDING UNSUBSCRIBING
@Reto and @codef0rmer had quite rightly pointed out that, as per the official docs, an unsubscribe()
inside the components onDestroy()
method is unnecessary in this instance. This has been removed from my code sample. (see blue alert box in this tutorial)
When a URL is like this http://stackoverflow.com?param1=value
You can get the param 1 by the following code:
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
@Component({
selector: '',
templateUrl: './abc.html',
styleUrls: ['./abc.less']
})
export class AbcComponent implements OnInit {
constructor(private route: ActivatedRoute) { }
ngOnInit() {
// get param
let param1 = this.route.snapshot.queryParams["param1"];
}
}
Even though the question specifies version beta 7, this question also comes up as top search result on Google for common phrases like angular 2 query parameters. For that reason here's an answer for the newest router (currently in alpha.7).
The way the params are read has changed dramatically. First you need to inject dependency called Router
in your constructor parameters like:
constructor(private router: Router) { }
and after that we can subscribe for the query parameters on our ngOnInit
method (constructor is okay too, but ngOnInit
should be used for testability) like
this.router
.routerState
.queryParams
.subscribe(params => {
this.selectedId = +params['id'];
});
In this example we read the query param id from URL like example.com?id=41
.
There are still few things to notice:
params
like params['id']
always returns a string, and this can be converted to number by prefixing it with +
.I really liked @StevePaul's answer but we can do the same without extraneous subscribe/unsubscribe call.
import { ActivatedRoute } from '@angular/router';
constructor(private activatedRoute: ActivatedRoute) {
let params: any = this.activatedRoute.snapshot.params;
console.log(params.id);
// or shortcut Type Casting
// (<any> this.activatedRoute.snapshot.params).id
}
import { Router } from '@angular/router';
this.router.navigate([ '/your-route' ], { queryParams: { key: va1, keyN: valN } });
import { ActivatedRoute } from '@angular/router';
this.activatedRoute.queryParams.subscribe(params => {
let value_1 = params['key'];
let value_N = params['keyN'];
});
Official source
Hi you can use URLSearchParams, you can read more about it here.
import:
import {URLSearchParams} from "@angular/http";
and function:
getParam(){
let params = new URLSearchParams(window.location.search);
let someParam = params.get('someParam');
return someParam;
}
Notice: It's not supported by all platforms and seems to be in "EXPERIMENTAL" state by angular docs
Get URL param as an object.
import { Router } from '@angular/router';
constructor(private router: Router) {
console.log(router.parseUrl(router.url));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With