Javascript 如何以一般方式处理“可能未处理的拒绝:背景点击”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42416570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle 'Possibly unhandled rejection: backdrop click' in a general way
提问by Andi
I have an angular service for handling modals:
我有一个处理模态的角度服务:
angular.module('myApp').service('ModalService', function($uibModal) {
function open(options) {
return $uibModal.open(options);
}
});
Now I upgraded to angular 1.6 and got this error:
现在我升级到 angular 1.6 并收到此错误:
Possibly unhandled rejection: backdrop click
可能未处理的拒绝:背景点击
whenever I open a modal and click somewhere else (the backdrop) and the modal closes (as intended). So I want to handle this unhandled exceptionin my ModalServiceas 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.
每当我打开一个模态并单击其他地方(背景)并且模态关闭(按预期)。所以我想处理这个问题unhandled exception,ModalService因为我不想每次使用ModalService. 通过背景点击关闭模态总是可以的,这也不例外。
I tried:
我试过:
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 clickas they are always thrown:
但这导致了我无法处理其他错误的问题,backdrop click因为它们总是被抛出:
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
});
And if I try it like this:
如果我像这样尝试:
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?
有没有人对此案有好的解决方案?
回答by Veglos
Unfortunately that's how they handle it in The official Plucker for Modal (ui.bootstrap.modal).
不幸的是,这就是他们在The official Plucker for Modal (ui.bootstrap.modal) 中处理它的方式。
If you click on any button it logs something like this:
如果您单击任何按钮,它会记录如下内容:
Modal dismissed at: Thu Feb 23 2017 21:54:26 GMT-0300 (Pacific SA Daylight Time)
模态被驳回:2017 年 2 月 23 日星期四 21:54:26 GMT-0300(南太平洋夏令时)
What they do is:
他们所做的是:
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:
如果您删除错误回调,请猜猜您会得到什么:
Possibly unhandled rejection: backdrop click
可能未处理的拒绝:背景点击
And even on cancel
甚至取消
Possibly unhandled rejection: cancel
可能未处理的拒绝:取消
So far, you either do that or use this workaround to silence unhandled rejections
到目前为止,您要么这样做,要么使用此解决方法来消除未处理的拒绝
app.config(['$qProvider', function ($qProvider) {
$qProvider.errorOnUnhandledRejections(false);
}]);
回答by jitendra varshney
use this
用这个
$uibModal.open({
////your code......
}).result.then(function(){}, function(res){})
now it will not give you error
现在它不会给你错误
回答by gBerger101
UI Specification dependent.
依赖于 UI 规范。
This is NOT the greatest workaround if there are UI specifications that the end-user must be able to click OUTSIDE the modal to close the modal.
如果有 UI 规范要求最终用户必须能够在模态外单击以关闭模态,那么这不是最好的解决方法。
If that is NOT the case and there is a little 'x' on the top right of the modal and/or there is a close
如果情况并非如此,并且模态右上角有一个小“x”和/或有一个关闭
backdrop: false, // <<< !!!!!!! (see code below)
will prevent the end-user from clicking OUTSIDE the modal to close the modal.
背景:假,// <<< !!!!!!!!! (请参阅下面的代码)
将阻止最终用户单击模态外以关闭模态。
$scope.change = function (changeableData, p_Mode) {
var modalInstance = $uibModal.open({
templateUrl: whatever,
controller: ModalInstanceCtrl,
scope: $scope,
backdrop: false, // <<< !!!!!!!
resolve: {
// whatever
}
});
This will prevent the error "Possibly unhandled rejection: backdrop click" from occurring.
这将防止发生错误“可能未处理的拒绝:背景点击”。
Once again, you need to look at the UI specifications and/or get permission from the analysts to implement this.
再一次,您需要查看 UI 规范和/或获得分析师的许可才能实现这一点。
回答by Malcor
If you're using a controller within your modal. I used this on the closing event. Because 'Closing' is valid but 'Dismissing' is a rejection. This goes within your modal controller, not the parent.
如果您在模态中使用控制器。我在闭幕式上使用了这个。因为“Closing”是有效的,而“Dismissing”是拒绝。这在您的模态控制器中,而不是父级。
$scope.$on('modal.closing', (event, reason, closed) => {
if (!closed) {
event.preventDefault();
$scope.$close("Closing");
}
});
So your backdrop click will fire the closing event but closed will be passed false. If that's the case, prevent the default behaviour, and programmatically close the modal instead of dismiss. Bare in mind this will break use of dismiss, if you want to use it for its original purpose.
所以你的背景点击将触发关闭事件,但关闭将被传递假。如果是这种情况,请防止默认行为,并以编程方式关闭模式而不是关闭。请记住,如果您想将其用于其原始目的,这将破坏dismiss 的使用。

