prevent AngularJS的关闭基于逻辑模态控制器内模 [英] Prevent AngularJS modal from closing based on logic inside the modal controller
问题描述
有没有办法来prevent自销的模式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.closing事件,广播模态的范围,就像这样:
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();
}
});
})
第一个参数是的事件的,它可以的 preventDefault()的从收prevent它。
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)
第三个参数是一个的布尔的指示模式是否被关闭或撤销。
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.
这篇关于prevent AngularJS的关闭基于逻辑模态控制器内模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!