Search code examples
angularangular2-routing

Pass param to Angular 2 (RC4) application


I would like to initially pass parameters to my Angular2 application via url query parameters. So if a user types http://myapp.com?r=www.stackoverflow.com into the address bar of the browser I would like to get the value of parameter r to store it in sessionStorage and use it later. I do not want to pass that value around from component to component.

What is the correct way to do that? Do I need to configure it in my routes and if so, how can I do that?

    export const routes: RouterConfig = [
       { path: ':r', component: LandingComponent },
    ....

That did not work or I could not figure out how to get the value.

Thanks for your Help!


Solution

  • I finaly solved it by getting document injected into the constructor

    constructor(@Inject(DOCUMENT) private document: any)
    

    and then getting the parameters as in vanilla JavaScript from

    this.document.location.search
    

    That works for me but I'm not sure if it works in all environments Angular2 can run on.