Ramesh Rajendran
Ramesh Rajendran

Reputation: 38683

What is the Angular2 equivalent to an AngularJS $routeChangeStart?

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

Upvotes: 3

Views: 846

Answers (1)

Jota.Toledo
Jota.Toledo

Reputation: 28434

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

Upvotes: 4

Related Questions