Hi all,
My issue is window.onbeforeunload is triggering twice and so dialog window is shown twice which should not happen. i have tried all possible ways to prevent that event to fire but nothing is working. so planned to handle the navigation using angular. Is there any way to handle the page unload in angular ?
I have tried using $destroy, but its not working..
Thanks in advance


Vinay SinghRank #604 · 4K reputationPosted Jul 19, 2016, 1:30 AM EDT
Prathaban Durai27 reputationPosted Jul 25, 2016, 10:16 PM EDT
$scope.currentLocation = $location.url();
$scope.$on("$locationChangeSuccess", function handleLocationChangeStartEvent(event) {
$scope.currentLocation = $location.url();
})
var startWatchingTimer = $timeout(startWatchingForLocationChanges, 0, false);
var stopWatchingLocation = null;
function handleLocationChangeStartEvent(event) {
event.preventDefault();
var targetPath = $location.path();
if ($scope.currentLocation != targetPath)
{
if (unsaved) {
var temp = confirm("Do you want to leave the page? /n All the unsaved data will be lost")
if (temp) {
$location.path(targetPath);
stopWatchingLocation();
$scope.$applyAsync(startWatchingForLocationChanges);
}
else {
window.stop();
}
}
}
}
//
function startWatchingForLocationChanges() {
stopWatchingLocation = $scope.$on("$locationChangeStart", handleLocationChangeStartEvent)
}