Tampa Tampa - 3 months ago 55
TypeScript Question

How to get query parameters in Angular 2 RC4

How does one get query parameters in Angular 2 RC4? I tried the docs and they make no sense whatsoever.

e.g.

http://localhost:3000/models/initials/mid_c1e1c31e6eff42f1982af4124a823b36?test=4


In
ngOnInit
how do I get the value of the parameter
test
?

private sub: Subscription;
ngOnInit() {

this.sub = this.route.params.subscribe(params => {

console.log(params);
let id = params['test'];
console.log(id);

});

}


This is how I navigate to the route where I want to get the query parms:

this._router.navigate(['models/initials/'+model_id+'?test=4']);

Answer
 constructor(private route:ActivatedRoute) {}

 ngOnInit() {
        this.route.params.map(params => params['test'])
            .subscribe(id => { /* do what you want with the id */ });