编程语言
首页 > 编程语言> > javascript-如何在用户离开时显示警告消息而不保存更改

javascript-如何在用户离开时显示警告消息而不保存更改

作者:互联网

我写了一条指令,监视$locationChangeStart事件,如果表单有未保存的更改,则会向用户显示一条消息.问题是事件处理程序永远不会触发.这是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!');
            }                

        });


    }]);

})();

然后,我标记要触发警告的表单:

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

notify-on-location-change.js包含在index.html中

我在这里想念什么?

解决方法:

将表单属性更改为“位置更改时通知”

详情请参见https://stackoverflow.com/a/19503227/584846

标签:angularjs-directive,javascript
来源: https://codeday.me/bug/20191111/2018998.html