如何以一般方式处理“可能未处理的拒绝:背景点击" [英] How to handle 'Possibly unhandled rejection: backdrop click' in a general way

查看:28
本文介绍了如何以一般方式处理“可能未处理的拒绝:背景点击"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处理模态的角度服务:

I have an angular service for handling modals:

angular.module('myApp').service('ModalService', function($uibModal) {
  function open(options) {
    return $uibModal.open(options);
  }
});

现在我升级到 angular 1.6 并收到此错误:

Now I upgraded to angular 1.6 and got this error:

可能未处理的拒绝:背景点击

Possibly unhandled rejection: backdrop click

每当我打开一个模态并单击其他地方(背景)时,模态就会关闭(按预期).所以我想在我的 ModalService 中处理这个 unhandled exception,因为我不想每次使用 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;
  }
});

但这会导致我无法处理除了 backdrop click 之外的其他错误,因为它们总是被抛出:

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?

推荐答案

不幸的是,他们在 中就是这样处理的Modal 的官方 Plucker (ui.bootstrap.modal).

如果你点击任何按钮,它会记录如下内容:

If you click on any button it logs something like this:

Modal 被驳回:2017 年 2 月 23 日星期四 21:54:26 GMT-0300(南太平洋夏令时间)

Modal dismissed at: Thu Feb 23 2017 21:54:26 GMT-0300 (Pacific SA Daylight Time)

他们所做的是:

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

甚至取消

可能未处理的拒绝:取消

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);
        }]);

这篇关于如何以一般方式处理“可能未处理的拒绝:背景点击"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆