2017-10-11 66 views
1

我遇到的问题是有点难以解释,我可能会得到(角)承诺不对,但仍...

我想很好地处理了下面的情况。 一般来说,假设我想让我的Angular dialogService提供一个confirm方法,该方法将返回一个单击yes按钮时解析的承诺,这意味着确认实际上成功了。但是,我希望该对话框会保持打开状态,直到内部async操作 - 将在yes确认中执行 - 完成。如果成功完成,那么对话框应该关闭,否则保持打开状态。

在代码,将(完全)看起来像这样:

外码:

dialogService.confirm('title', 'message').then => 
     return myLongLastingOperationReturningPromise() 

confirm方法的实现是这样的:

def = $q.defer() 

    dialog = ngDialog.open(...) 
    // closePromise or any other custom local promise 
    dialog.closePromise.then => 
     // this is fake, but how can I achieve this? 
     result = def.resolve('closeRequest'); 
     if(typeof result.then == 'function') { 
      result.then => 
       // continue closing the dialog 
     } else if (result === false) { 
      // just do nothing 
     } else { 
      // closing the dialog 
     } 
换句话说

,有没有办法在拨打resolve之后得到Promise链中最后一个then方法的结果?

回答

0

您应该在API成功返回后执行确认。

例如 点击 '是' 按钮,将执行方法YesHandler:

//$scope is the ngDialog scope here 
$scope.YesHandler = function() { 
    myLongLastingOperationReturningPromise().then(function(data) { 
    //Execute confirm method after API returned 
    $scope.confirm(data); 
    }) 
} 

在来电者:

modalInstance = ngDialog.openConfirm({ 
        template: 'xxx.tpl.html', 
        scope: $scope, 
        controller: 'xxxCtrl' 
        }); 

modalInstance.then(function (data) { 
    //This data is returned from confirm 
}); 
+0

嗨,你检查这个答案? –