How to show a warning message when a user exits without saving changes

I wrote a directive that monitors events $locationChangeStart

and shows the user a message if the form has unsaved changes. The problem is that the event handler never runs. Here is a simplified version of my code innotify-on-location-change.js

    (function() {
    'use strict';


    angular.module('myApp.directives').directive('notifyOnLocationChange', ['$scope', 'event', 'next', 'current', function ($scope, event, next, current) {

        $scope.$on('$locationChangeStart', function () {                
            if (true) {
                alert('Unsaved data!');
            }                

        });

        $scope.$on('$stateChangeStart', function () {                
            if (true) {
                alert('Unsaved data!');
            }                

        });


    }]);

})();

      

Then I mark the form where I want to trigger the alert:

 <form name="detailsForm" ng-submit="$ctrl.onSubmit()" novalidate notifyOnLocationChange>

      

notify-on-location-change.js

included in index.html

What am I missing here?

+3


source to share


1 answer


Change the shape attribute to notify-on-location-change



See fooobar.com/questions/447225 / ... for details.

+3


source







All Articles