2017-10-11 99 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

嗨,你檢查這個答案? –

相關問題