How to deregister $rootScope.$on in app.run

42 Views Asked by At

I am broadcasting an event in my app controller:

var locationSrt = location.toString();
if(locationSrt.indexOf("?RecoverPassword") !== -1) {
    $rootScope.$broadcast('recover-password');
}

Then in my app.js in app.run I have $rootScope.$on (from what I read app.run doesn't support $scope)

app.run(function ($rootScope, $location) {
    $rootScope.$on('recover-password', function () {
        $location.path("/recover-password");
    });
});

The problem is $rootScope.$on is called every time in login controller even though condition locationSrt.indexOf("?RecoverPassword") !== -1 is no longer true as the location.search is set to $window.location.search = '';

How can I prevent $rootScope.$on from being called again?

0

There are 0 best solutions below