我编写了一个指令,监听$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中。
我在这里缺少什么?