10

我在Angular中使用来自Bootstrap UI(https://angular-ui.github.io/bootstrap/)的模态组件来呈现模态,并且在关闭时我希望能够重定向到另一个状态,或者至少有一个函数被调用。

问题是我可以拦截close事件,但是每当我用户按下Esc模式时,模式就会被解除,并且我找不到任何关于捕获dismiss事件或将函数分配给返回的承诺的文档。

代码如下:

var modalInstance = $uibModal.open({
  templateUrl: 'a-template.html',
  controller: 'AnotherCtrl',
  controllerAs: 'modal',
  backdrop: false,
  size: 'lg'
});

// I am able to catch this whenever the user exits the modal in an
// orthodox fashion. Doesn't happen when he presses Esc button.
modalInstance.closed = function () {
  $state.go(homeState); 
};

或者,适合我的业务案例的另一种解决方案是根本不允许用户关闭模式。并在模态控制器中发生事件时自行关闭它。到目前为止,我还没有找到这方面的功能。

4

1 回答 1

23

在与模态关联的控制器中(“AnotherCtrl”或“模态”对你来说),你可以$scope.$on()'modal.closing'事件一起使用。

例子:

var modalInstance = $uibModal.open({
  templateUrl: 'a-template.html',
  controller: ['$scope', function($scope){
    $scope.$on('modal.closing', function(event, reason, closed){
        if('condition for not closing')
            event.preventDefault(); //You can use this to prevent the modal from closing            
        else
            window.location = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";            
    });
  }],
  controllerAs: 'modal',
  backdrop: false,
  size: 'lg'
});
于 2016-03-17T16:48:23.343 回答