0

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

这是我的代码:

角度模块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; 
    }


});

有任何想法吗?

4

1 回答 1

0

尝试这个

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

         e.preventDefault();
         return;
    }
于 2016-11-09T18:20:21.500 回答