As you've discovered above, you can use a combination of window.onbeforeunload
and $locationChangeStart
to message the user. In addition, you can utilize ngForm.$dirty
to only message the user when they have made changes.
I've written an angularjs directive that you can apply to any form that will automatically watch for changes and message the user if they reload the page or navigate away. @see https://github.com/facultymatt/angular-unsavedChanges
Hopefully you find this directive useful!