When user entered some data in form and tried to close browser window accidentally, system has to show alert message to save their data. This example is to implement this functionality using AngularJS. Observe below two functions
<input ng-change="setConfirmUnload(true)" ng-model="data"/>
<button ng-click="saveData()">Save Data</button>
<div ng-bind="data1"></div>
$window.onbeforeunload will trigger the message from assigned function. Here you need call $scope.setConfirmUnload(true) whenever user enters data in form and call $scope.setConfirmUnload(false)  whenever user save the form data.
$scope.setConfirmUnload = function(on) {
    window.onbeforeunload = (on) ? $scope.unloadMessage : null;

$scope.unloadMessage = function() {
    return "You have unsaved changes";
$scope.saveData = function() {
    $scope.data1 = $scope.data;



Popular Posts