MacKenzie Software

How to get param from URL in Angular

Posted by MacKenzie Software on June 10, 2020

As part of in interactive application it is common to need to get one or more parameters from the URL, for example getting an authentication token. Here is how you do this in Angular.

Firstly, import ActivatedRoute:

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

Then in your constructor you can pull the parameter. For example:

constructor(private route: ActivatedRoute) { 
    this.route.queryParams.subscribe(params => {
        let myparam = params['myparam'];
        console.log(myparam); // Print the parameter to the console.  
    });
}

It's as easy as that! Hope this helps.