简体   繁体   English

如何在用户离开时不显示更改时显示警告消息

[英]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. 我编写了一个监视$locationChangeStart事件的指令,并在表单有未保存的更改时向用户显示一条消息。 The problem is that the event handler is never triggered. 问题是永远不会触发事件处理程序。 Here is simplified version of my code in notify-on-location-change.js 这是我在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 notify-on-location-change.js包含在index.html中

What am I missing here? 我在这里错过了什么?

Change the form attribute to notify-on-location-change 将表单属性更改为notify-on-location-change

See https://stackoverflow.com/a/19503227/584846 for more details 有关详细信息,请参阅https://stackoverflow.com/a/19503227/584846

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户离开页面而不提交表单时如何显示警告消息 - how to show Warning message when user leaves the page without submitting the form 当用户离开窗口而不保存数据时提供自定义消息 - Provide custom message when user leaves window without saving data 离开页面而不保存时的警告消息 - Warning message when leaving a page without saving 当用户移动到另一个页面而不保存数据时如何向用户显示通知 - How to show a notification to user when user moves to another page without saving data Alpaca Forms验证仅在用户离开字段后显示错误消息(离开字段) - Alpaca Forms validation show error message only after user leaves field (leaves the field) 如何使用onunload / onbeforeunload检查用户何时离开页面。 知道是否阅读消息 - how to use onunload/onbeforeunload to check when user leaves page. To know if message is read 当按钮被禁用时,如何显示消息或警告模式? - How to show up message or warning modal , when button is disabled? 从插件向用户显示警告(NOT error)消息 - Show warning (NOT error) message to user from plugin 如何检测用户何时离开网站? - How to detect when a user leaves the site? 用户离开输入时如何添加事件监听器 - How to add eventlistener when user leaves input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM