Angularjs 当AngularUI引导模式被取消且动画已完成执行时调用函数

Angularjs 当AngularUI引导模式被取消且动画已完成执行时调用函数,angularjs,twitter-bootstrap,angular-ui-bootstrap,Angularjs,Twitter Bootstrap,Angular Ui Bootstrap,我正在使用Angular UI引导模式,我遇到了一点问题 我想在引导模式动画完成时调用一个函数。下面的代码块将在模态开始解除时调用cancel()函数,而不是在模态解除动画完成时 Angular UI不使用事件,因此不会触发“hidden.bs.modal”事件(至少据我所知) 当模式开始关闭时,cancel()块立即运行。我需要代码在引导模式的关闭动画完成时执行 如何使用angular UI实现这一点 供参考的组件: 谢谢 有点晚了,但希望它仍然有帮助!您可以劫持uib模态窗口指令,并检查其

我正在使用Angular UI引导模式,我遇到了一点问题

我想在引导模式动画完成时调用一个函数。下面的代码块将在模态开始解除时调用cancel()函数,而不是在模态解除动画完成时

Angular UI不使用事件,因此不会触发“hidden.bs.modal”事件(至少据我所知)

当模式开始关闭时,cancel()块立即运行。我需要代码在引导模式的关闭动画完成时执行

如何使用angular UI实现这一点

供参考的组件:


谢谢

有点晚了,但希望它仍然有帮助!您可以劫持uib模态窗口指令,并检查其作用域何时被破坏(它是一个独立的作用域指令)。当模式最终从文档中删除时,范围将被销毁。我还将使用服务来封装功能:

服务

app.service('Modals', function ($uibModal, $q) {
  var service = this,
      // Unique class prefix
      WINDOW_CLASS_PREFIX = 'modal-window-interceptor-',
      // Map to save created modal instances (key is unique class)
      openedWindows = {};

  this.open = function (options) {
    // create unique class
    var windowClass = _.uniqueId(WINDOW_CLASS_PREFIX);

    // check if we already have a defined class
    if (options.windowClass) {
      options.windowClass += ' ' + windowClass;
    } else {
      options.windowClass = windowClass;
    }

    // create new modal instance
    var instance = $uibModal.open(options);

    // attach a new promise which will be resolved when the modal is removed
    var removedDeferred = $q.defer();
    instance.removed = removedDeferred.promise;

    // remember instance in internal map
    openedWindows[windowClass] = {
      instance: instance,
      removedDeferred: removedDeferred
    };
    return instance;
  };

  this.afterRemove = function (modalElement) {
    // get the unique window class assigned to the modal
    var windowClass = _.find(_.keys(openedWindows), function (windowClass) {
        return modalElement.hasClass(windowClass);
      });

    // check if we have found a valid class
    if (!windowClass || !openedWindows[windowClass]) {
      return;
    }

    // get the deferred object, resolve and clean up
    var removedDeferred = openedWindows[windowClass].removedDeferred;
    removedDeferred.resolve();
    delete openedWindows[windowClass];
  };

  return this;
});
app.directive('uibModalWindow', function (Modals) {
  return {
    link: function (scope, element) {
      scope.$on('$destroy', function () {
        Modals.afterRemove(element);
      });
    }
  }
});
指令

app.service('Modals', function ($uibModal, $q) {
  var service = this,
      // Unique class prefix
      WINDOW_CLASS_PREFIX = 'modal-window-interceptor-',
      // Map to save created modal instances (key is unique class)
      openedWindows = {};

  this.open = function (options) {
    // create unique class
    var windowClass = _.uniqueId(WINDOW_CLASS_PREFIX);

    // check if we already have a defined class
    if (options.windowClass) {
      options.windowClass += ' ' + windowClass;
    } else {
      options.windowClass = windowClass;
    }

    // create new modal instance
    var instance = $uibModal.open(options);

    // attach a new promise which will be resolved when the modal is removed
    var removedDeferred = $q.defer();
    instance.removed = removedDeferred.promise;

    // remember instance in internal map
    openedWindows[windowClass] = {
      instance: instance,
      removedDeferred: removedDeferred
    };
    return instance;
  };

  this.afterRemove = function (modalElement) {
    // get the unique window class assigned to the modal
    var windowClass = _.find(_.keys(openedWindows), function (windowClass) {
        return modalElement.hasClass(windowClass);
      });

    // check if we have found a valid class
    if (!windowClass || !openedWindows[windowClass]) {
      return;
    }

    // get the deferred object, resolve and clean up
    var removedDeferred = openedWindows[windowClass].removedDeferred;
    removedDeferred.resolve();
    delete openedWindows[windowClass];
  };

  return this;
});
app.directive('uibModalWindow', function (Modals) {
  return {
    link: function (scope, element) {
      scope.$on('$destroy', function () {
        Modals.afterRemove(element);
      });
    }
  }
});
并在控制器中使用它,如下所示:

app.controller('MainCtrl', function ($scope, Modals) {
  $scope.openModal = function () {
    var instance = Modals.open({
      template: '<div class="modal-body">Close Me</div>' +
        '<div class="modal-footer"><a class="btn btn-default" ng-click="$close()">Close</a></div>'
    });

    instance.result.finally(function () {
      alert('result');
    });

    instance.removed.then(function () {
      alert('closed');
    });
  };
});
app.controller('MainCtrl',函数($scope,Modals){
$scope.openModal=函数(){
var实例=Modals.open({
模板:“关闭我”+

“.

我使用设置为动画完成所需时间的超时来破解它。我完全不喜欢这种想法,因为如果模式关闭的时间发生变化,那么超时也需要更新。这是一个非常好的解决方案!我会尝试一下。非常感谢!我只想说,下一版本的UI引导程序希望您执行以下操作:
instance.closed.then(function(){…}
。我遇到的问题是angular UI引导程序的下一个版本将不支持angular 1.3,这将关闭我的项目。