VOOZH about

URL: https://www.geeksforgeeks.org/angular-js/how-to-detect-a-route-change-in-angularjs/

⇱ How to detect a route change in AngularJS ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to detect a route change in AngularJS ?

Last Updated : 6 Sep, 2022

In this article, we will see how to detect a route change in AngularJS. In order to detect route change at any moment in AngularJS, this can be achieved by using the $on() method. The $on() method is an event handler, the event which will handle $routeChangeSuccess which gets triggered when route/view change is done. 

Syntax: 

$rootScope.$on('$routeChangeSuccess', function () {
 Content...
});

Approach: Here, we are displaying "route changed" in the console window whenever there is any route change. Inside the $on() method, we console route changed. Hence, In this way whenever route change occurs, it triggers $routeChangeSuccess handled by $on() event handler, and then "route changed" is displayed in the console window.

Example: This example describes the process to detect a route change in AngularJS.

Output: As we change the link, the $routeChangeSuccess event gets triggered, and thus displays route changes in the console window.

👁 Image
 
Comment

Explore