简体   繁体   English

在诸如窗口onbeforeunload的AngularJs中调用$ routeChangeStart之前显示确认模式

[英]Show confirmation modal before calling $routeChangeStart in AngularJs like window onbeforeunload

Im able to show a confirmation modal once route starts to change, if users selects to stay my route does not change, it stays to the original route, but loads the form directives again, which causes form to loss all its checkbox, input values. 一旦路线开始更改,我将无法显示确认模态,如果用户选择保留我的路线,则它会保持原始路线,但会再次加载表单指令,这会导致表单丢失其所有复选框和输入值。 It gets reset to defaults. 它重置为默认值。

If a user closes the page, i'm able to show confirmation modal, also the form state does not change. 如果用户关闭页面,则我可以显示确认模式,并且表单状态也不会更改。 All values are retained. 保留所有值。

Below is my code: also, please note Im also injecting a resolve for all routes by default (this Angular : How use one resolve for all the routes of my application ) 以下是我的代码:另外,请注意,默认情况下,我还为所有路由注入了一个解析(此Angular:如何为我的应用程序的所有路由使用一个解析

After that im calling .run() 之后我打电话给.run()

$rootScope.$on('$routeChangeStart', function (event, newUrl, oldUrl) {
            //isLoggedIn also check URL params
            if (!Auth.isLoggedIn()) {
                $window.location.href = Auth.getLoginUrl();
            }

            ////unsaved modal start
            if ($rootScope.unsaved) {

                ngDialog.open({
                    className: 'ngdialog-theme-default unsaved-modal',
                    template: 'scripts/core/commonmodal/tplUnsavedModal.html',
                    controller: [function OpenDialogContainer() {
                        var modal = this;
                        modal.message = 'You have some unsaved changes. Do you want to leave this page?';
                        modal.stop = function () {
                            modal.processing = true;
                            ngDialog.close();
                            $rootScope.$broadcast('$routeChangeSuccess');
                        };
                        modal.continue = function () {
                            modal.processing = true;
                            ngDialog.close();
                            $rootScope.unsaved = false;
                            $location.path(newUrl.$$route.originalPath); //Go to page they're interested in
                        };
                    }],
                    controllerAs: 'modal'
                });
                //prevent navigation by default since we'll handle it
                //once the user selects a dialog option
                event.preventDefault();
            }

        });

Im setting $rootScope.unsaved = true if form is NOT $pristine and NOT $submitted 我设置$rootScope.unsaved = true如果表单不是$pristine并且不是$submitted

As you can see in the below gifvideo, on stay the route runs the controller function again. 正如您在下面的gif视频中看到的那样,在停留期间,路线再次运行控制器功能。 Instead what I wanted was a window onbeforeunload alike effect. 相反,我想要的是一个类似window onbeforeunloadwindow onbeforeunload

http://recordit.co/g5T9wWkDry.gif http://recordit.co/g5T9wWkDry.gif

I fixed it just by removing this line $rootScope.$broadcast('$routeChangeSuccess'); 我通过删除此行$rootScope.$broadcast('$routeChangeSuccess'); also, instead I made a directive. 另外,我也做了一个指令。

link: function($scope) {
                var message = 'You have some unsaved changes. Do you want to leave this page?';
                $window.onbeforeunload = function(){
                    if ($scope.unsavedChanges) {
                        return 'You have some unsaved changes. Do you want to leave this page?';
                    }
                };
                var $routeChangeStartUnbind = $rootScope.$on('$routeChangeStart', function(event, newUrl) {
                    if ($scope.unsavedChanges) {
                        $rootScope.pageloaded = true;//prevent loading icon
                        ngDialog.open({
                            appendClassName: 'unsaved-modal',
                            template: 'scripts/core/commonmodal/tplUnsavedModal.html',
                            controller: [function OpenDialogContainer() {
                                var modal = this;
                                modal.message = message;
                                modal.stop = function () {
                                    modal.processing = true;
                                    ngDialog.close();
                                };
                                modal.continue = function () {

                                    modal.processing = true;
                                    ngDialog.close();
                                    $routeChangeStartUnbind();
                                    $location.path(newUrl.$$route.originalPath); //Go to page they're interested in
                                    $rootScope.pageloaded = false;
                                    $scope.unsavedChanges = false;
                                    $window.location.reload();//$route.reload() does not reload services
                                };
                            }],
                            controllerAs: 'modal'
                        });
                        //prevent navigation by default since we'll handle it
                        //once the user selects a dialog option
                        event.preventDefault();
                    }
                });

                $scope.$on('$destroy', function() {
                    window.onbeforeunload = null;
                    $routeChangeStartUnbind();
                });
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM