【问题标题】:Event not waiting for user answer inside dialog事件不等待对话框内的用户回答
【发布时间】:2016-11-09 17:50:00
【问题描述】:

如果满足特定条件,我需要用户确认离开页面。问题是位置更改没有等待对话框得到用户回答。

这是我的代码:

角度模块 1:

...
function confirmLeavePage(e, newUrl) {
        if(form.cod.value) {
            customDialog.confirmDialog('Title','Leave?').then(
            function(){
                console.log('go to selected page');
            },function(){
                e.preventDefault();
            });
        }
    }

    $scope.$on("$locationChangeStart", confirmLeavePage);
...

角度模块 2:

angular.module('dialog').service('customDialog', function($mdDialog, $q, $location) {

    this.confirmDialog = function(title, content){
        var deferred = $q.defer();
        $mdDialog.show($mdDialog.confirm({
            templateUrl:'confirmDialog.html',
            title : title,
            textContent : content,
            ok : 'Confirm',
            cancel: 'Cancel'
        })).then(function() {
            console.log('confirmed');
            deferred.resolve();
        }, function() {
           console.log('abort');
           deferred.reject();
        });
        return deferred.promise; 
    }


});

有什么想法吗?

【问题讨论】:

    标签: angularjs events ngroute preventdefault mddialog


    【解决方案1】:

    试试这个

    function confirmLeavePage(e, newUrl) {
            if(form.cod.value) {
                customDialog.confirmDialog('Title','Leave?').then(
                function(){
                    console.log('go to selected page');
                });
            }
    
             e.preventDefault();
             return;
        }
    

    【讨论】:

    • 我用$location.path(newUrl) 替换了console.log('go to selected page');,但它再次触发了对话框,因为它被$scope.$on("$locationChangeStart", confirmLeavePage); 捕获了任何想法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多