根据模态控制器内部的逻辑,防止AngularJS模态关闭

Mar*_*ark 2 angularjs angular-ui angular-bootstrap

有没有办法防止AngularJS模态在模态的控制器逻辑中关闭/解除?

我想要的是当用户关闭模态时显示警告,并且模态内的表单包含未保存的数据.

我已经尝试过搜索一个关闭之前的事件或者我可以在官方文档中使用的其他东西,但到目前为止还没有运气.

Vic*_*mes 7

您可以观看'modal.closing'事件,广播到模态的范围,如下所示:

.controller('modalCtrl', function($scope, $modalInstance) {

  $scope.$on('modal.closing', function(event, reason, closed) {
      var r = prompt("Are you sure you wanna close the modal? (Enter 'YES' to close)");

      if (r !== 'YES') {
        event.preventDefault();
      }
  });
})
Run Code Online (Sandbox Code Playgroud)

第一个参数是事件,您可以使用preventDefault()来阻止它关闭.

第二个参数是原因(无论传递给$ close()方法)

第三个参数是一个布尔值,表示模态是关闭还是解除.

这是一个工作的plunker

我不知道何时添加,但目前在官方文档中提到.