So I have an AngularJS service listening for some events. On the handling of these events I need to call different controllers and ultimately load a new view. In one event handler I am using $location.path() then calling $rootScope.apply() to trigger the routing to the controller. This works fine for that event, but in others I get the following error: $rootScope:inprog Action Already In Progress
. I'm guessing that it works in the first scenario because $rootScope.apply() is called from another callback function inside the listener function, where as the other handlers try to call it just from the event listener function.
//angular service
$rootScope.$on('MY_EVENT', function (event, msg) {
MyClass.doSomething(msg, function (response) {
$location.path("/view1");
$rootScope.$apply(); //WORKS FINE
});
});
$rootScope.$on('MY_OTHER_EVENT', function (event, msg) {
$location.path("/view2");
$rootScope.$apply(); //ERROR
});
How can I get it to work for all event handlers?