9

Using ngRoute once can hook up in event: $routeChangeStart and do different actions ...

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

Is it possible to achieve the same using UI-Router?

David Dury
  • 5,537
  • 12
  • 56
  • 94

3 Answers3

16

Yes it's possible:

$rootScope.$on("$stateChangeStart",
    function (event, toState, toParams, fromState, fromParams) {
karaxuna
  • 26,752
  • 13
  • 82
  • 117
1

Or just use

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

If you want this to be triggered on a single page.

Petru
  • 904
  • 1
  • 12
  • 18
0

Check this answer here, that's the correct way to fix it. Removing all listeners could have unknown effects as there might be other places where listeners are added. You need to remove the one you added, not all of them.

Check this issue: Angular $rootScope $on listeners in 'destroyed' controller keep running

Copying 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);
});
Stef
  • 2,603
  • 2
  • 18
  • 28