我编写了一个指令来监视 $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 中
我在这里错过了什么?
最佳答案
将表单属性更改为notify-on-location-change
关于javascript - 如何在用户离开而不保存更改时显示警告消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44295356/