ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript – 如何以一般方式处理’可能未处理的拒绝:背景点击’

2019-10-07 17:46:55  阅读:279  来源: 互联网

标签:javascript angularjs error-handling angular-ui-bootstrap angular-ui


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

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

现在我升级到角度1.6并得到此错误:

Possibly unhandled rejection: backdrop click

每当我打开一个模态并单击其他地方(背景)并且模态关闭(按预期).所以我想在我的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;
  }
});

但这会导致我无法处理除背景点击之外的其他错误的问题,因为它们总是被抛出:

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

它解决了“未处理的拒绝”错误,但对于每种情况,不仅仅是“背景点击”.

有没有人为这个案子提供一个好的解决方案?

解决方法:

不幸的是,他们在The official Plucker for Modal (ui.bootstrap.modal)处理它的方式.

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

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

如果你删除错误回调,猜猜你得到了什么:

Possibly unhandled rejection: backdrop click

甚至取消

Possibly unhandled rejection: cancel

到目前为止,您要么这样做,要么使用此解决方法来消除未处理的拒绝

app.config(['$qProvider', function ($qProvider) {
            $qProvider.errorOnUnhandledRejections(false);
        }]);

标签:javascript,angularjs,error-handling,angular-ui-bootstrap,angular-ui
来源: https://codeday.me/bug/20191007/1868396.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有