3

In AngularJS we were able to specify route change event to observe changes in route object using the $routeChangeStart/End event of the $rootScope. What is the equivalent of the route change event in Angular2?


how can we do this exact functionality of below code in Angular2

 $scope.$on('$routeChangeStart', function (scope, next, current) {
        //do what you want
      });

I got some disucussions here, But it don't have more details, So I asked a new question.

angular2 $routeChangeStart , $routeChangeSuccess ,$routeChangeError

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234

1 Answers1

4

You can listen the events of the router by doing the following:

import {
  Router, ActivatedRoute,
  NavigationEnd, NavigationStart,
  NavigationError, NavigationCancel,
} from '@angular/router';

// constructor method of some angular element
constructor(
    private _router: Router,
  ) {
this._router.events
          .filter(event => event instanceof NavigationStart)
          .subscribe(event => {
            console.log("New route");
          });
 }

EDIT: Im not completely sure is that is actually what you need, after taking a closer look to the angularjs docs seems like those events are more related to the resolution/result of a guard in angular2

Jota.Toledo
  • 27,293
  • 11
  • 59
  • 73