And*_*ndi 21 javascript error-handling angularjs angular-ui angular-ui-bootstrap
我有处理模态的角度服务:
angular.module('myApp').service('ModalService', function($uibModal) {
function open(options) {
return $uibModal.open(options);
}
});
Run Code Online (Sandbox Code Playgroud)
现在我升级到角度1.6并得到此错误:
可能是未处理的拒绝:背景点击
每当我打开一个模态并单击其他地方(背景)并且模态关闭(按预期).所以我想unhandled exception在我处理这个,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;
}
});
Run Code Online (Sandbox Code Playgroud)
但这会导致我无法处理其他错误的问题,而不是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
});
Run Code Online (Sandbox Code Playgroud)
如果我这样尝试:
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;
});
});
Run Code Online (Sandbox Code Playgroud)
它解决了"未处理的拒绝"错误,但对于每种情况,不仅仅是"背景点击".
有没有人为这个案子提供一个好的解决方案?
小智 21
不幸的是,他们是如何处理官方的Plucker for Modal(ui.bootstrap.modal).
如果你点击任何按钮它会记录如下:
Modal被解雇:2017年2月23日星期四21:54:26 GMT-0300(太平洋SA日光时间)
他们做的是:
modalInstance.result.then(function (selectedItem) {
$ctrl.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
Run Code Online (Sandbox Code Playgroud)
如果你删除错误回调,猜猜你得到了什么:
可能是未处理的拒绝:背景点击
甚至取消
可能是未处理的拒绝:取消
到目前为止,您要么这样做,要么使用此解决方法来消除未处理的拒绝
app.config(['$qProvider', function ($qProvider) {
$qProvider.errorOnUnhandledRejections(false);
}]);
Run Code Online (Sandbox Code Playgroud)
用这个
$uibModal.open({
////your code......
}).result.then(function(){}, function(res){})
Run Code Online (Sandbox Code Playgroud)
现在它不会给你错误
依赖于 UI 规范。
如果有 UI 规范要求最终用户必须能够在模态外单击以关闭模态,那么这不是最好的解决方法。
如果情况并非如此,并且模态右上角有一个小“x”和/或有一个关闭
背景:假,// <<< !!!!!!!!! (请参阅下面的代码)
将阻止最终用户单击模态外以关闭模态。
$scope.change = function (changeableData, p_Mode) {
var modalInstance = $uibModal.open({
templateUrl: whatever,
controller: ModalInstanceCtrl,
scope: $scope,
backdrop: false, // <<< !!!!!!!
resolve: {
// whatever
}
});
Run Code Online (Sandbox Code Playgroud)
这将防止发生错误“可能未处理的拒绝:背景点击”。
再一次,您需要查看 UI 规范和/或获得分析师的许可才能实现这一点。
| 归档时间: |
|
| 查看次数: |
19148 次 |
| 最近记录: |