Angularjs $locationChangeStart事件在更改位置时命中两次
当窗体变脏并且我正在更改位置时,我将触发确认模式指令弹出窗口Angularjs $locationChangeStart事件在更改位置时命中两次,angularjs,angularjs-directive,Angularjs,Angularjs Directive,当窗体变脏并且我正在更改位置时,我将触发确认模式指令弹出窗口 angular.module("top.app.controllers").directive('confirmOnExit', ['$location', 'ConfirmModal', '$timeout',function (location, ConfirmModal, $timeout) { return { restrict: 'A', link: function ($scope
angular.module("top.app.controllers").directive('confirmOnExit', ['$location', 'ConfirmModal', '$timeout',function (location, ConfirmModal, $timeout) {
return {
restrict: 'A',
link: function ($scope, element, attrs) {
$scope.$evalAsync(function () {
var unbindChangeSuccess = $scope.$on('$locationChangeStart', function (event, next, current, e) {
$scope.DirtyForm = ($scope.componentAddForm.$dirty ? $scope.componentAddForm.$dirty : $scope.resourceForm.$dirty)
if ($scope.DirtyForm) {
event.preventDefault();
ConfirmModal.show({
okButtonCaption: "OK",
cancelButtonCaption: "Cancel",
title: "Pending Changes",
text: "Any unsaved data to this record will be lost. Click ‘Continue’ to proceed with this action."
}).then(function () {
event.preventDefault();
$timeout(function () {
window.location = next;
$scope.$apply()
});
})
} else {
};
});
})
}
};
}]);
你有module.config吗?请把它复制到这里。另外,复制出现问题的任何html视图。我怀疑您有一个路由定义,并且您已经在视图中放置了ng控制器。当您使用routes时,控制器由route解决,因此您可以省略ng控制器标记,否则位置将更改两次(route和view)。我认为@zameb是正确的。通常,当某个对象被调用两次时,这是因为控制器被定义了两次。当这件事发生在我身上时,是因为他提到的原因。你能告诉我们你要去的路线的html和js代码是什么吗?我已经删除了控制器,这个问题仍然存在,我在所有的视图中都使用了它,导致了相同的问题。不,我没有任何module.config我有bundle.config@zameb你有module.config吗?请把它复制到这里。另外,复制出现问题的任何html视图。我怀疑您有一个路由定义,并且您已经在视图中放置了ng控制器。当您使用routes时,控制器由route解决,因此您可以省略ng控制器标记,否则位置将更改两次(route和view)。我认为@zameb是正确的。通常,当某个对象被调用两次时,这是因为控制器被定义了两次。当这件事发生在我身上时,是因为他提到的原因。你能告诉我们你要去的路线的html和js代码是什么吗?我已经删除了控制器,这个问题仍然存在,我在所有的视图中都使用了它,导致了相同的问题。不,我没有任何module.config,我有bundle.config@zameb