0

Is there a way to only listen for the VERY first route start and none of the others? Obvs this could be done with a variable but was wondering if there was an event for this instead.

       $rootScope.$on("$routeChangeStart", function (event, next, current) {
            Auth.authorize().then(function(){
                // only want to happen the first time
                // $location.path('dlp');
            },function(){
                $location.path('login');
            });
        });
amcdnl
  • 8,470
  • 12
  • 63
  • 99

1 Answers1

3

You can unsubscribe from $rootScope.$on broadcasts if you assign it to a variable and call it as a function:

var offCallMeFn = $rootScope.$on("$routeChangeStart", function(event, next, current){
        Auth.authorize().then(function(){
            // only want to happen the first time
            // $location.path('dlp');
        },function(){
            $location.path('login');
        });
});

 //this will deregister that listener
offCallMeFn();

For handling authorization this seems like a bad idea however as users might access routes manually (e.g. from browser history) or possibly logout.

Source: How to unsubscribe to a broadcast event in angularJS. How to remove function registered via $on

Community
  • 1
  • 1
hugo der hungrige
  • 12,382
  • 9
  • 57
  • 84