AngularJS Route Change Events

Overview

In this article, we will see Route Change events in AngularJS. We will see this with an example of how different events are triggered when a route change occurs in an angular application.

Introduction

In this example we will be looking at, 
  • Different events that are triggered when a route change occurs in an angular application.
  • Logging the event and event handler parameters to inspect their respective properties.

When a route change events occurs the following events are triggered,

  • $locationChangeStart
  • $routeChangeStart
  • $locationChangeSuccess
  • $routeChangeSuccess

Let’s see with an example  -- we will be using the same example. We will work with the student’s controller,

Now here will inject $rootscope object and $log service in our student’s controller,

  1. .controller("studentsController"function ($http, $route, $rootScope,$log) {  
  2.                
  3.                   var vm = this;  
  4.                   vm.reloadData = function () {  
  5.                       $route.reload();  
  6.                   }  
  7.                   $http.get("StudentService.asmx/GetAllStudents")  
  8.                   .then(function (response) {  
  9.                       vm.students = response.data;  
  10.                   })  
  11.                     })  
Now we will write some code to handle these four events so copy paste this in your controller as,
  1. $rootScope.$on("$locationChangeStart"function () {  
  2.                       $log.debug("$locationChangeStart is fired");  
  3.                   });  
  4.                   $rootScope.$on("$routeChangeStart"function () {  
  5.                       $log.debug("$routeChangeStart is fired");  
  6.                   });  
  7.                   $rootScope.$on("$locationChangeSuccess"function () {  
  8.                       $log.debug("$locationChangeSuccess is fired");  
  9.                   });  
  10.                   $rootScope.$on("$routeChangeSuccess"function () {  
  11.                       $log.debug("$routeChangeSuccess is fired");  
  12.                   });  
As you can see we are handling those four events here notice what these event handlers are doing --  they are just logging into their events.

Save the changes and reload the page and launch browser developer tools,

tools

At the moment we are on Student controller; when I click on home controller we will see what happens,

tools

As you can see when I clicked on home controller the various events are fired.

Now we will use three parameters in the function event, next and current which we had seen in the article. Cancel event Routes in Angular JS and also instead of using $rootScope we will use $scope object.
  1. .controller("studentsController"function ($http, $route, $scope,$log) {  
  2.               
  3.                  var vm = this;  
  4.   
  5.                  $scope.$on("$locationChangeStart"function (event, next, current) {  
  6.                      $log.debug("$locationChangeStart is fired");  
  7.                  });  
  8.                  $scope.$on("$routeChangeStart"function (event, next, current) {  
  9.                      $log.debug("$routeChangeStart is fired");  
  10.                  });  
Also we log the event parameters too,
  1. .controller("studentsController"function ($http, $route, $scope,$log) {  
  2.             
  3.                var vm = this;  
  4.   
  5.                $scope.$on("$locationChangeStart"function (event, next, current) {  
  6.                    $log.debug("$locationChangeStart is fired");  
  7.                    $log.debug(event);  
  8.                    $log.debug(next);  
  9.                    $log.debug(current);  
  10.                });  
  11.                $scope.$on("$routeChangeStart"function (event, next, current) {  
  12.                    $log.debug("$routeChangeStart is fired");  
  13.                    $log.debug(event);  
  14.                    $log.debug(next);  
  15.                    $log.debug(current);  
  16.                });  
Now let’s save the changes and reload the page. Initially I am in student’s controller and will click on courses,

tools

So click on Courses Controller,

Controller

Expand the first event you will get the detailed information:

code

In the next parameter you will get the route information 

code

The information as loadedtemplateurl is template/courses.html which is correct. The next parameter is $$route when you expand that,

code

Now here it contains the original path;  the original path is /courses, the URL with which we are navigating to the next route.

The third parameter which contains the information about the parent parameter so click on that

code

The current parameter has $$route and also has an original path as,

code

And then it has locationChangeStart event,

code

Conclusion

So this was all about Route Change events in AngularJS. Hope this article was helpful!!

Up Next
    Ebook Download
    View all
    Learn
    View all