michali
michali

Reputation: 3862

Angular 2.0 - How to get query params

Supposing I have this following URL - http://localhost:4200?runID=5555

How can I pass query param "runID" to my app.component:

import { Component } from '@angular/core';
import {OnInit} from '@angular/core';
@Component({...})
export class AppComponent {}

Upvotes: 1

Views: 111

Answers (1)

Supamiu
Supamiu

Reputation: 8731

You can get queryParams using the current ActivatedRoute:

export class AppComponent implements OnInit{

    constructor(private route: ActivatedRoute) {
    }

    ngOnInit():void{
         this.route.queryParams.subscribe(params => {
             console.log(params);
         //params is an object here, it should be {runID:5555}
         });
    }
}

route.queryParams is an Observable<any> that will emit each time the query params are changed and thus will emit once the dom is initialized (that's why I subscribe in the ngOnInit() method).

More informations on route.queryParams in anguar.io docs.

Upvotes: 2

Related Questions