Angular-返回$modal promise并拒绝它
Angular - return $modal promise and reject it
我正试图编写一个函数,打开一个有角度的ui模式窗口,并在关闭后返回promise。这通常很容易,你必须返回模态实例,但在这种情况下,即使窗口是关闭的属性,我也希望检查.then()函数内的响应,并可能根据状态码拒绝promise,即使它已经成功。
这可能没有多大意义,所以这里有一些代码。。。
// 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
});
您可以返回一个新的promise,只有在$modalInstance.result中的promise得到解决并且您的状态代码检查良好时,该promise才会得到解决。大致如下:
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;
}
请参阅$q上的文档。
相关文章:
- 在ES6 Promise中,我应该在解决/拒绝之前使用return吗
- 在promise中为回调添加超时并拒绝
- angular ui bootstrap:当模式关闭时,promise被解析/拒绝
- JavaScript Promise 解析并拒绝函数调用序列
- 带有拒绝调用和抛出错误的Promise构造函数
- Bluebird Promise.any() 提前拒绝
- 节点 js,使用带有 Q promise 的猫鼬不调用拒绝函数
- 在ES6 Promise中允许多次解析/拒绝的原因是什么
- 拒绝promise时停止代码执行
- AngularJS/Karma-测试函数返回已解决或拒绝的promise
- 当你想要所有结果而不管是否被拒绝时,用什么代替Promise.all()
- Dojo.request.post-返回的promise说"拒绝”;,但服务器似乎已经接受了请求
- Bluebird Promise.all-多个承诺完成了成功和拒绝的汇总
- Angular-返回$modal promise并拒绝它
- node.js Q promise不会通过拒绝处理程序传播异常
- 当action使用promise时,indexOf拒绝在值突变中工作
- 的承诺.all - resolve回调会被触发,尽管Promise被拒绝
- 稍后在TypeScript中解析或拒绝Promise
- 在promise .race中第一个promise被解析(拒绝)后执行是否结束
- new Promise和Promise的区别.在bluebird中解决/拒绝