user469033
user469033

Reputation:

How to get the route parameter in Angular 2? (RC - stable - router)

I'm using the new stable Angular 2 RC version. The documentation for the new router component has not been written yet. I am struggling to simply get a parameter from a navigated to page.

My routes:

@Routes([
  {path: '/results', component: ResultListComponent},
  {path: '/result-detail/:id', component: ResultDetailComponent}
]) 

I am navigating to my detail component using:

this.router.navigate(['/result-detail', result.primary_id]);

finally I don't know how to get the primary_id in the result-detail component, using a 'RouteSegment'?

Upvotes: 1

Views: 393

Answers (1)

Morteza Manavi
Morteza Manavi

Reputation: 33216

In Angular RC1 Router, you need to have your component implement OnActivate interface to get the route parameters:

import { Router, OnActivate, RouteSegment, RouteTree } from '@angular/router';


Component({
...
})
export class ResultDetailComponent implements OnActivate {
    constructor() { }

    routerOnActivate(curr: RouteSegment, prev?: RouteSegment, currTree?: RouteTree,
                     prevTree?: RouteTree) {
        this.id = curr.getParam('id'),
}

Upvotes: 1

Related Questions