$ locationChangeStart事件在更改位置时命中两次

时间:2017-09-08 14:25:31

标签: angularjs angularjs-directive

当表单变脏并且我正在更改位置时,我正在触发确认模式指令弹出窗口。

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 {
                    };
                });

            })
        }
    };
}]);

0 个答案:

没有答案