【问题标题】:Return value from modal Angular-UI bootstrap even if an error has occurred即使发生错误,也从模态 Angular-UI 引导程序返回值
【发布时间】:2016-01-14 03:49:47
【问题描述】:

我已经阅读了很多关于如何在 modal Bootstrap 中捕获错误的问题,但没有一个可以解释在这种情况下来自 modal 的返回值。

此代码调用模态:

var modalInstance = $modal.open({
        animation: true,
        templateUrl: 'dummy/test',
        controller: 'modalController',
        size: 'lg',
    }).result.then(function (result) {
        $scope.numAttachs = result;
    }, function (error) {
        // return value???
    });

modalController 中:

/*... Some logic ...*/

// Button returns values. 
$scope.saveValues = function () {
    $scope.$close($scope.numAttachs);  // This works fine
}
// Event triggered when modal controller destroy
$scope.$on("$destroy", function () {
    //$scope.$close($scope.numAttachs); // nope...
    // Here I would return value
});

我的目的是在所有情况下都返回变量 numAttachs

  • 用户按下保存按钮。 (我明白了)
  • 用户在背景中按下。 (我不明白)
  • 用户按下 esc 键。 (我不明白)

我不喜欢在解决方案中暗示事件“$destroy”,这是一种绝望的措施。我想有任何方法可以捕获错误并获得返回值,但我找不到它。 当我在 function (error) { // return value ?} variable error 中捕获错误时,会采用“escape key press”或“backdrop click”的值,但没有办法返回值。有什么建议吗?

感谢您的宝贵时间!

【问题讨论】:

    标签: angularjs angular-ui-bootstrap bootstrap-modal


    【解决方案1】:

    不幸的是,如果不禁用背景单击或退出键按下,则无法将可配置值传递回 reject 函数处理程序 - 也就是说,唯一可接受的解决方案是您自己调用 dismiss()escape key pressbackdrop click 这两条消息是硬编码的。

    我可以看到允许用户指定返回的可选值的用例,但这对于团队的其他成员来说可能很难卖。我会问的。在此期间,有人认为您可以做,是的,它很臭,将您的信息存储在注入到代码中的服务中,您在该代码中根据开放模式操作返回的承诺进行操作。我知道,我知道......它很臭。

    【讨论】:

    • 很遗憾...我提出这个问题是为了让我确定它无法完成。
    【解决方案2】:

    其他情况不允许用户按 esc 键或背景放置:

    backdrop : 'static',   // to prevent popup close by backdrop click
    keyboard :false        // to prevent popup close by esc key
    

    用户只能通过我可以返回值的按钮出去。

    【讨论】:

      猜你喜欢
      • 2021-04-27
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      • 2019-08-30
      • 2021-12-29
      • 2020-04-29
      相关资源
      最近更新 更多