Javascript 如何处理';可能未经处理的拒绝:点击&x27;大体上
我有一个处理情态动词的角度服务:Javascript 如何处理';可能未经处理的拒绝:点击&x27;大体上,javascript,angularjs,error-handling,angular-ui-bootstrap,angular-ui,Javascript,Angularjs,Error Handling,Angular Ui Bootstrap,Angular Ui,我有一个处理情态动词的角度服务: angular.module('myApp').service('ModalService', function($uibModal) { function open(options) { return $uibModal.open(options); } }); 现在我升级到angular 1.6,出现了以下错误: 可能未处理的拒绝:单击 每当我打开一个模态并单击其他地方(背景)时,模态就会关闭(如预期的那样)。因此,我想在我的ModalSe
angular.module('myApp').service('ModalService', function($uibModal) {
function open(options) {
return $uibModal.open(options);
}
});
现在我升级到angular 1.6,出现了以下错误:
可能未处理的拒绝:单击
每当我打开一个模态并单击其他地方(背景)时,模态就会关闭(如预期的那样)。因此,我想在我的
ModalService
中处理这个未处理的异常
,因为我不想每次使用ModalService
时都处理这种情况。通过背景单击关闭模式总是可以的,这也不例外
我试过:
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;
}
});
但这导致了一个问题,除了background 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
});
如果我这样尝试:
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;
});
});
它解决了“未处理的拒绝”错误,但对于每种情况,不仅仅是“未处理的拒绝”
有人能为这个案子找到好的解决办法吗?不幸的是,他们就是这样处理的 如果单击任何按钮,它会记录如下内容:
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;
});
});
时间:2017年2月23日星期四21:54:26 GMT-0300(南太平洋夏令时)
他们所做的是:
modalInstance.result.then(function (selectedItem) {
$ctrl.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
如果删除错误回调,猜猜会得到什么:
可能未处理的拒绝:单击
甚至取消
可能未处理的拒绝:取消
到目前为止,您要么这样做,要么使用此解决方案来消除未处理的拒绝
app.config(['$qProvider', function ($qProvider) {
$qProvider.errorOnUnhandledRejections(false);
}]);
用这个
$uibModal.open({
////your code......
}).result.then(function(){}, function(res){})
现在,它不会给您错误依赖于UI规范 如果存在最终用户必须能够在模式外部单击以关闭模式的UI规范,则这不是最好的解决方法
$scope.change = function (changeableData, p_Mode) {
var modalInstance = $uibModal.open({
templateUrl: whatever,
controller: ModalInstanceCtrl,
scope: $scope,
backdrop: false, // <<< !!!!!!!
resolve: {
// whatever
}
});
如果情况并非如此,且模态右上角有一个小“x”和/或有一个闭合
背景:错误,//如果您使用的是模态中的控制器。我在闭幕式上用过这个。因为“关闭”是有效的,但“解雇”是拒绝。这在模态控制器内,而不是父控制器内
$scope.$on('modal.closing', (event, reason, closed) => {
if (!closed) {
event.preventDefault();
$scope.$close("Closing");
}
});
因此,您的背景单击将触发关闭事件,但关闭将被传递为false。如果是这种情况,请防止默认行为,并以编程方式关闭模态,而不是关闭。请记住,如果您想将其用于最初的目的,这将中断discover的使用 嗯,所以我所能做的就是通过配置或捕获它们并吞下它们来消除错误?这并不令人满意……我同意。但这就是核心的行为方式。至少(希望)到1.6.2。请注意,您不应该使错误静音,而是使用第二个参数
处理被拒绝的承诺,然后
“我不想每次使用ModalService时都处理这种情况”,但是如果您使用app.config(['$qProvider',function($qProvider){$qProvider.errorOnUnhandledRejections(false),这是angularjs的问题; }]); 然后许多错误将无法处理,因为我希望用户能够单击背景以关闭模式,这不是解决方案(正如您在介绍中已经指出的)。谢谢,这对我很有帮助。我有一个关闭按钮和一个X来关闭窗口,所以对于我的特殊情况,最好不要让用户点击背景。