当用户离开页面而没有保存更改时如何显示警告信息

3

我编写了一个指令,监听$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中。

我在这里缺少什么?

1个回答

3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接