Created
November 13, 2015 19:49
-
-
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)
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
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