Last active
April 1, 2016 17:32
-
-
Save shaik2many/6dc1dafd30f53b33e2f4973e01208100 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
//Confirm page leave if the form is dirty | |
$scope.$on('$locationChangeStart', function( event ) { | |
if($scope.addTaxEntryForm.$dirty) { | |
var answer = confirm("Are you sure you want to leave this page?") | |
if (!answer) { | |
event.preventDefault(); | |
} | |
} | |
}); | |
$window.onbeforeunload = function (e) { | |
var confirmation = {}; | |
var event = $rootScope.$broadcast('onBeforeUnload', confirmation); | |
if (event.defaultPrevented) { | |
return confirmation.message; | |
} | |
}; | |
$scope.$on('onBeforeUnload', function (e, confirmation) { | |
confirmation.message = ""; | |
e.preventDefault(); | |
}); | |
*/ | |
<!DOCTYPE html> | |
<html data-ng-app="TestApp"> | |
<head> | |
<script src="http://code.angularjs.org/1.2.9/angular.js"></script> | |
<script> | |
angular.module('TestApp', []) | |
.factory('beforeUnload', function ($rootScope, $window) { | |
// Events are broadcast outside the Scope Lifecycle | |
$window.onbeforeunload = function (e) { | |
var confirmation = {}; | |
var event = $rootScope.$broadcast('onBeforeUnload', confirmation); | |
if (event.defaultPrevented) { | |
return confirmation.message; | |
} | |
}; | |
$window.onunload = function () { | |
$rootScope.$broadcast('onUnload'); | |
}; | |
return {}; | |
}) | |
.run(function (beforeUnload) { | |
// Must invoke the service at least once | |
}); | |
function TestController($scope) { | |
$scope.$on('onBeforeUnload', function (e, confirmation) { | |
confirmation.message = "All data willl be lost."; | |
e.preventDefault(); | |
}); | |
$scope.$on('onUnload', function (e) { | |
console.log('leaving page'); // Use 'Preserve Log' option in Console | |
}); | |
} | |
</script> | |
</head> | |
<body data-ng-controller="TestController"> | |
This is a test | |
<a href="http://www.google.com/">Google</a> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment