AngularJS UL Router Event Change Route

Using ngRoute may one day connect to the event: $routeChangeStart and perform different actions ...

  app.run(function ($rootScope, $location) { $rootScope.$on("$routeChangeStart", function (event, next, current) { ................ 

Is it possible to achieve this using a UI router?

+6
source share
3 answers

Yes, it is possible:

 $rootScope.$on("$stateChangeStart", function (event, toState, toParams, fromState, fromParams) { 
+12
source

Or just use

 $scope.$on("$stateChangeStart",...); 

If you want this to be run on the same page.

+1
source

Check this answer here to fix it correctly. Removing all listeners may have unknown effects, as there may be other places where listeners are added. You need to delete the one you added, not all.

Check this problem: Angular $ rootScope $ on listeners in the "destroyed" controller continues to work

Copy the code here for completeness too:

 animateApp.controller('mainController', function($scope, $rootScope, service) { $scope.pageClass = 'page-home'; var unregister = $rootScope.$on('service.abc', function (newval) { console.log($scope.$id); }); $scope.$on('$destroy', unregister); }); 
0
source

Source: https://habr.com/ru/post/984999/


All Articles