从关闭延迟 angular-ui 模态

Delay angular-ui modal from closing

我正在使用 angularjsangular-ui 库(特别是 angular-ui-bootstrap 库)。

如何延迟关闭模式?在用户点击 x 或取消按钮后,我需要让模态窗口多显示半秒,但我还没有找到实现它的方法。

我想在传给模态实例then函数的回调函数中加一些超时时间,比如:

modalInstance.result.then(function success(){
    // timeout .5 second
}, function cancel() {
   // timeout .5 second
});

但回调函数中的代码实际上是 运行 在模态窗口关闭之后。有办法吗?

您可以使用

 $timeout(function() {
     $modalInstance.dismiss('cancel');
 }, 500);

Plunker

带有独立模态控制器的完整代码:-

angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($scope, $modal, $log) {

  $scope.items = ['item1', 'item2', 'item3'];

  $scope.open = function (size) {

    var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: 'ModalInstanceCtrl',
      size: size,
      resolve: {
        items: function () {
          return $scope.items;
        }
      }
    });

    modalInstance.result.then(function (selectedItem) {
      $scope.selected = selectedItem;
    }, function () {
      $log.info('Modal dismissed at: ' + new Date());
    });
  };
});

// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.

angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $modalInstance,$timeout, items) {

  $scope.items = items;
  $scope.selected = {
    item: $scope.items[0]
  };

  $scope.ok = function () {
    $modalInstance.close($scope.selected.item);
  };

  $scope.cancel = function () {
    $timeout(function() {
            $modalInstance.dismiss('cancel');
        }, 500);

  };
});