我编写了一个监视$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
我在这里缺少什么?
答案 0 :(得分:3)
将表单属性更改为notify-on-location-change