根据模态控制器内部的逻辑防止 AngularJS 模态关闭 [英] Prevent AngularJS modal from closing based on logic inside the modal controller

查看:24
本文介绍了根据模态控制器内部的逻辑防止 AngularJS 模态关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to prevent the an AngularJS modal from closing/dismissing in the controller logic of the modal?

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

What I want is to show a warning when the users closes the modal and the form inside the modal contains unsaved data.

我试过在 中搜索关闭前事件或其他我可以使用的内容官方文档,但到目前为止还没有运气.

I've tried searching for a before close event or something else I could use in the official documentation, but no luck so far.

推荐答案

您可以观看 'modal.closure' 事件,广播到 modal 的作用域,如下所示:

You can watch the 'modal.closing' event, broadcasted to the modal's scope, like this:

.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();
      }
  });
})

第一个参数是event,你可以preventDefault()来防止它关闭.

The first parameter is the event, which you can preventDefault() to prevent it from closing.

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

The second parameter is the reason (whatever is passed to the $close() method)

第三个参数是一个 boolean,指示模态是关闭还是关闭.

The third parameter is a boolean indicating whether the modal was closed or dismissed.

这里有一个工作 plunker

我不知道这是什么时候添加的,但目前官方文档中提到了.

I don't know when this was added, but currently it is mentioned in the official documentation.

这篇关于根据模态控制器内部的逻辑防止 AngularJS 模态关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆