(Update) Finding current URL with Angular 2.0 Routing

23 בJune 2016

no comments

Previously I’ve posted about finding current URL with Angular 2.0 Routing which was suitable for rc-1.

On Angular 2.0 rc-3, the angular router (alpha 3.0.0) has been added to the repository with much easier
API for getting the current URL.

Let’s have a look at the following code (Typescript)

   1: constructor(private _router:Router){


   3:     }


   5:     ngOnInit():any {

   6:        this._router['routerEvents'].subscribe(event => {

   7:           if (event.url){

   8:               this.currentRoute = event.url;

   9:           }

  10:        });

  11:     }

What I did here was subscribe to the router events. There are some router events which we can subscribe to. Two of them are NavigationStart and NavigationEnd . The event instance can me constructed from either one of them, however, yet we cannot determine which one   so although we’re interested in NavigatioEnd this code will run twice every route.


Nevertheless, we can (in much either way than  the way presented in rc-1) get the current URL.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>