首页 文章

Angular - 返回$ modal承诺并拒绝它

提问于
浏览
1

我正在尝试编写一个函数,它打开一个angular-ui模态窗口,并在它关闭后返回一个promise . 这通常很简单,你返回模态实例,但在这种情况下,即使窗口是关闭属性,我想检查.then()函数内的响应,并可能根据状态代码拒绝承诺,即使它已经成功了 .

这可能没什么意义,所以这里有一些代码......

// Function which should return a promise
var myFunc = function(){

    var modalInstance = $modal.open({
        templateUrl: 'xxx',
        controller: 'yyy',
    });

    return modalInstance.result.then(function (result) {

        // If the login was successful then resolve
        if(result && result.success === true){

            // What goes here?

        }

        // If it was uncuccessful then reject, even though it was closed successfully.
        else{

            // What goes here?

        }


    }, function () {
        $log.info('Modal was closed unsuccessfully');
    });


}


myFunc.then(function(){
    // DO something
});

1 回答

  • 2

    您可以返回一个新的承诺,只有在$ modalInstance.result的承诺得到解决并且您的状态代码检查良好时才会得到解决 . 有点像:

    var myFunc = function(){
    
        var modalInstance = $modal.open({
            templateUrl: 'xxx',
            controller: 'yyy',
        });
    
        var deferred = $q.defer();
        modalInstance.result.then(function() {
            if (/* status code checks ok */) {
                deferred.resolve();
            }
            else {
                deferred.reject();
            }
        }, function() { 
            deferred.reject();
        });
    
        return deferred.promise;
    }
    

    docs on $q .

相关问题