Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I wrote a directive that watches for $locationChangeStart events and shows the user a message if the form has unsaved changes. The problem is that the event handler is never triggered. Here is simplified version of my code in notify-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!');
            }                

        });


    }]);

})();

I then mark the form I want to trigger the warning:

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

notify-on-location-change.js is included in index.html

What am I missing here?

like image 596
ventsyv Avatar asked Nov 28 '25 22:11

ventsyv


1 Answers

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

See https://stackoverflow.com/a/19503227/584846 for more details

like image 187
Brent Washburne Avatar answered Nov 30 '25 12:11

Brent Washburne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!