Angular2 get url query parameters Angular2 get url query parameters angular angular

Angular2 get url query parameters


You're looking for query parameters from ActivatedRoute.

To receive them, you could put them into your component's OnInit function like this:

private accesstoken;private code;constructor(private route: ActivatedRoute) { }ngOnInit() {  // Capture the access token and code  this.route      .queryParams      .subscribe(params => {          this.accesstoken = params['#access_token'];          this.code = params['code'];      });  // do something with this.code and this.accesstoken}

There's more examples in the link I put above. You may have problems with angular because the router also identifies fragments, which start with the #... If angular does identify it as a fragment, then you can just subscribe to the fragment observable from ActivatedRoute and do it that way.

I'm not sure if you're being redirected. If you are, the could look into using the preserveQueryParams navigation option, something like this.

this.router.navigate([redirect], {preserveQueryParams: true});


Here is my final working code:

Imports:

import { Router, NavigationCancel } from '@angular/router';import { URLSearchParams, } from '@angular/http';

Constructor:

 constructor(public router: Router) {    router.events.subscribe(s => {      if (s instanceof NavigationCancel) {        let params = new URLSearchParams(s.url.split('#')[1]);        let access_token = params.get('access_token');        let code = params.get('code');      }    });  }


using Javascript :

(new URL(location)).searchParams.get("parameter_name")