angular.module('myApp.directives', []).directive('confirmOnExit', function() {
return {
link: function($scope, elem, attrs) {
$scope.$on('$locationChangeStart', function(event, next, current) {
alert('me');
if ($scope.myForm.$dirty) {
if(!confirm("Ahh the form is dirty, do u want to continue?")) {
event.preventDefault();
}
}
});
window.onbeforeunload = function(){
alert('me');
if ($scope.myForm.$dirty) {
return "The Form is Dirty.";
}
}
}
};
});
Its not working with browser back button when having unsaved data during intra page navigation.But works with browser close and reload. Any pointers where i may be going wrong.Or is dere any other way to get this working.
Here is the link to stackoverflow also : : http://stackoverflow.com/questions/14892103/window-onbeforeunload-not-working-for-intra-page-navigation-in-angularjs-app