简体   繁体   English

从 $http promise 以编程方式关闭模态对话框

[英]Close modal dialog programmatically from $http promise

I'm using Bootstrap UI in conjunction with AngularJS to show a modal dialog, while a $http request is processing.我将Bootstrap UI与 AngularJS 结合使用来显示模式对话框,同时处理$http请求。 Think of it as a wait dialog.将其视为等待对话框。 I'd like to keep the code as a simple four-liner that I can drop in at various locations like this:我想将代码保留为一个简单的四行代码,我可以像这样在不同的位置放置它:

$scope.foobar = function() {
  var waitDialog = $uibModal.open({ templateUrl: 'waitDialog.html' });
  waitDialog.result.catch(function() { /** Rejection ignored */ });

  MyService.doSomething()
  .then(function onSuccess(response) {
    waitDialog.dismiss('Success.');
    // ...
  })
  .catch(function onError(response) {
    waitDialog.dismiss('Failed.');
  });
};

This works fine most of the time , but every now and then the modal dialog does not close programmatically.在大多数情况下工作正常,但时不时地模式对话框不会以编程方式关闭。 It remains open, even though the HTTP request returned a result.它保持打开状态,即使 HTTP 请求返回了结果。 I've added a close button to the dialog, so users can still dismiss it by calling $dismiss() manually.我在对话框中添加了一个关闭按钮,因此用户仍然可以通过手动调用$dismiss()来关闭它。

I do not understand, however, why the dialog is not always dismissed programmatically.但是,我不明白为什么对话框并不总是以编程方式关闭。 I'm aware of the fact, that $http returns a promise and there might be a delay.我知道$http返回 promise 并且可能会有延迟。 But I have debugged the code by printing the waitDialog object to console and it always seems to be both, defined and the identical object. I don't see any scope problems.但是我通过将waitDialog object 打印到控制台来调试代码,它似乎总是既定义又相同的 object。我没有看到任何 scope 问题。 What am I missing?我错过了什么?

You cannot close modal before it is opened, so should be waitDialog.opened.then(() => waitDialog.dismiss())你不能在打开之前关闭模态,所以应该是waitDialog.opened.then(() => waitDialog.dismiss())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM