Skip to content

Instantly share code, notes, and snippets.

@edbentinck
Created November 13, 2015 19:49
Show Gist options
  • Save edbentinck/31f5aa2640630a127a7f to your computer and use it in GitHub Desktop.
Save edbentinck/31f5aa2640630a127a7f to your computer and use it in GitHub Desktop.
AngularJS save form before exit (both changing url or closing the window/tab)
angular.module('saveBeforeExitExample', [])
.directive("saveBeforeExit", ["notificationService", function(notificationService) {
"use strict";
return {
link: function(scope, element, attrs) {
window.onbeforeunload = function(){
if (element.hasClass("ng-dirty")) {
element.submit();
}
};
scope.$on("$locationChangeStart", function(event, next, current) {
if (element.hasClass("ng-dirty")) {
element.submit();
}
});
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment