Dheeraj Agrawal
Dheeraj Agrawal

Reputation: 2357

Get activated route from angular 2 routing

Hi so I have declared my route with the id like this {path: 'spreadsheet/:id', component: ContactParent} so here I can get the id by using ActivatedRoute but how I can get spreadsheet, if I do this.router.url it gives me spreadsheet/20 but I only need spreadsheet

Upvotes: 5

Views: 14917

Answers (4)

Jnr
Jnr

Reputation: 1664

Although you should be able to get the route from the ActivatedRoute, there may be times you want to get the route from your bootstrapped component. In this case you can use angular's Location Service and call location.path() to get the requested route.

Upvotes: 0

Shubham Jain
Shubham Jain

Reputation: 118

import {Router} from '@angular/router'; // import Router from @angular/router

constructor(private router: Router){ // private member of Router
 const currentPageUrl = router.url; // router.url contain the active route info
 console.log('Activated router is ' + currentPageUrl);
}

Upvotes: 1

Rohit N Arya
Rohit N Arya

Reputation: 1

Use like this

<a actionLink='['/list',id]'>link</a>


    constructor( private _route: ActivatedRoute ){ 
    let id=this._route.snapshot.param['id'];
    }

You have to import the ActivatedRoute.

Upvotes: -1

Joshua Craven
Joshua Craven

Reputation: 4545

Try this...

constructor( private route: ActivatedRoute ) {
    console.log(route.pathFromRoot[1].snapshot.url[0].path);
}

If you go to, for example, http://localhost:4200/spreadsheet/3, the above code will log "spreadsheet"

Upvotes: 1

Related Questions