I have the following callback for my onbeforeUnload event in one of my service.
In my app.run block I have:
window.onbeforeunload = Services.beforeWindowClose;
and this method is in a service:
this.beforeWindowClose = function (event) {
var currentState = $state.current.name;
if (currentState !== constant.LOGOUT) {
$rootScope.$broadcast("close");
if (Utilities.isSavePending)
event.returnValue = constant.MSG;
}
}
In my controllers I have :
$scope.$on("close", function () {
Utilities.isSavePending = vm.save; //sets it to true
})
Now given the events are synchronous in angular, this code should give me a popup on window close. However,this directly closes my window.
My intention is that whenever the user closes window, I raise an event and see if there is any unsaved data in my controller. If there is some unsaved data, the browser should not close and give a popup, while if there is no unsaved data, the browser should close.
Am I doing or understanding something wrong?