[英]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.