问题描述
我有处理模态的角度服务:
I have an angular service for handling modals:
angular.module('myApp').service('ModalService', function($uibModal) {
function open(options) {
return $uibModal.open(options);
}
});
现在我升级到角度1.6并收到此错误:
Now I upgraded to angular 1.6 and got this error:
每当我打开模态并点击其他地方(背景)和模态关闭(按预期)。所以我想在我的 ModalService
中处理这个未处理的异常
,因为我每次使用时都不想处理这种情况 ModalService
。通过背景点击关闭模态总是可以的,这也不例外。
whenever I open a modal and click somewhere else (the backdrop) and the modal closes (as intended). So I want to handle this unhandled exception
in my ModalService
as I do not want to handle this case everytime I use the ModalService
. It is always ok to close the modal via backdrop click, this is no exception.
我试过:
angular.module('myApp').service('ModalService', function($uibModal) {
function open(options) {
var modalInstance = $uibModal.open(options);
modalInstance.result.catch(function error(error) {
if(error === "backdrop click") {
// do nothing
} else {
throw error;
}
})
return modalInstance;
}
});
但这会导致我无法处理除背景点击之外的其他错误的问题
因为它们总是抛出:
But this leads to the problem that I cannot handle other errors than backdrop click
as they are always thrown:
ModalService.open({...}).result.catch(function(error) {
// this will catch the error too, but the throw in the ModalService
// will occure in parallel and will not be catched by this function
});
如果我这样尝试:
angular.module('myApp').service('ModalService', function($uibModal) {
function open(options) {
var modalInstance = $uibModal.open(options);
modalInstance.result.then(function(whatever) {
return whatever;
}, function rejection(error) {
return error;
});
return modalInstance;
});
});
它解决了未处理的拒绝错误,但是对于每种情况都不仅仅是背景点击。
it resolves the 'unhandled rejection' error, but for every case not just for 'backdrop clicked'.
这个案子有没有一个好的解决方案?
Has anybody a good solution for this case?
推荐答案
不幸的是这就是他们在中处理它的方法。
Unfortunately that's how they handle it in The official Plucker for Modal (ui.bootstrap.modal).
如果您点击任何按钮,它会记录如下内容:
If you click on any button it logs something like this:
他们做的是:
modalInstance.result.then(function (selectedItem) {
$ctrl.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
如果你删除错误回调,猜猜你得到了什么:
If you remove the error callback, guess what you get:
甚至取消
到目前为止,你要么这样做,要么使用此变通方法来消除未处理的拒绝
So far, you either do that or use this workaround to silence unhandled rejections
app.config(['$qProvider', function ($qProvider) {
$qProvider.errorOnUnhandledRejections(false);
}]);
这篇关于如何处理'可能未处理的拒绝:背景点击'一般方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!