我正在尝试编写一个函数,它打开一个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 回答
您可以返回一个新的承诺,只有在$ modalInstance.result的承诺得到解决并且您的状态代码检查良好时才会得到解决 . 有点像:
见docs on $q .