Angular 模态关闭功能未执行

Angular modal close function not executing

加载模式已正确创建,但是当点击 finally 块时它不会关闭它。这有什么已知的原因吗?加载时间很短,但在有延迟的情况下我仍然需要它。我正在使用 Chrome 中的设备进行测试 - 只有在 Chrome 中 运行 时才会出现问题。

$scope.init = function() {
  var dialog = Modals.openLoadingModal();

  OfflineManager.getTemplates().then(function(templates) {
    $scope.templates = templates.map(function(e) {
      // get e
      return e;
    });

    OfflineManager.getInspections().then(function(inspections) {
      $scope.inspections = inspections.map(function(e) {
        // get e
        return e;
      });
    }).finally(function() {
      dialog.close();
    });
  });
};

模态视图:

<div class="loadingModal">
  <data-spinner data-ng-init="config={color:'#fff', lines:8}" data-config="config"></spinner>
</div>

模态服务:

this.openLoadingModal = function(callback) {
  var opts = {
    backdrop: true,
    backdropClick: false,
    keyboard: false,
    templateUrl: 'views/modals/loading.html'
  };
  return this.open(opts, callback, null);
};


this.open = function(opts, closeHandler, dismissHandler, model) {
  opts.resolve = { modalModel:function() { return model; }};
  opts.controller = opts.controller || 'ModalController';

  $('div, input, textarea, select, button').attr('tabindex', -1);

  var modalInstance = $modal.open(opts);
  modalInstance.result.then(function(result) {
    $('div, input, textarea, select, button').removeAttr('tabindex');
    if (closeHandler) {
      closeHandler(result);
    }
  }, function(result) {
    $('div, input, textarea, select, button').removeAttr('tabindex');
    if (dismissHandler) {
      dismissHandler(result);
    }
  });
  return modalInstance;
};

经过一番搜索后,我找到了以下解决方案,该解决方案会等到模式完成打开后再执行:

.finally(function() {
    dialog.opened.then(function() {
      dialog.close();        
    });
});

来源: Call function after modal loads angularjs ui bootstrap

根据 ui.bootstrap 文档 - http://angular-ui.github.io/bootstrap/versioned-docs/0.13.3/#/modal

result - a promise that is resolved when a modal is closed and rejected when a modal is dismissed

您似乎在尝试使用错误的承诺来执行您的逻辑。 result 作为调用 $modalInstance.close$modalInstance.dismiss 的产物被触发。如果您尝试以编程方式关闭模态(而不是通过模态 template/controller 中的 ng-click 关闭),您需要直接调用 $modalInstance.close$modalInstance.dismiss然后你的result.then将执行。