Javascript 在模态关闭后捕获 Angular Bootstrap UI $uibModal 关闭事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38221783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
catching Angular Bootstrap UI $uibModal closed event after the modal was closed
提问by Avner Hoffmann
I'm opening a modal window using $uibModal.open from another controller, and need to be notified when the modal window was closed completely (and not during closing...) so I'll be able to run a function.
我正在使用 $uibModal.open 从另一个控制器打开一个模态窗口,并且需要在模态窗口完全关闭时(而不是在关闭期间...)收到通知,以便我能够运行一个函数。
The code that opens the modal is as follows:
打开模态的代码如下:
var modalInstance = $uibModal.open({
templateUrl: "myModalContent.html",
controller: "termModalCtrl",
windowClass: 'app-modal-window',
resolve: {
'params': function () { return id }
}
});
I saw some suggested solutions to use:
我看到了一些建议使用的解决方案:
modalInstance.result.then(function(result) {
});
The problem is that the function callback is called prior to the actual closing of the modal window (when the modal window is still open) and this is not the behavior I want cause it means that it catches the "closing" event and not the "closed" event of the modal.
问题是在实际关闭模态窗口之前调用函数回调(当模态窗口仍然打开时),这不是我想要的行为,因为它意味着它捕获“关闭”事件而不是“关闭”事件的模态。
Is there a neat and simple way to implement this? I'd be surprised if not since this behavior is very common in any UI frameworks on the planet...
有没有一种简洁而简单的方法来实现这一点?如果没有,我会感到惊讶,因为这种行为在地球上的任何 UI 框架中都很常见......
Please help!
请帮忙!
回答by Srijith
Try this.
尝试这个。
.open
method returns a promise that could be chained with .closed
which is one of the many properties of .open
method.
.open
方法返回一个可以链接的承诺,.closed
这是.open
方法的众多属性之一。
I tested it and it shows the alert only after the modal has closed and not while it's 'closing'.
我对其进行了测试,它仅在模态关闭后才显示警报,而不是在“关闭”时显示。
Refer the 'closed' under Returnsection here
请参阅此处的退货部分下的“已关闭”
var modalInstance = $uibModal.open({
templateUrl: "myModalContent.html",
controller: "termModalCtrl",
windowClass: 'app-modal-window',
resolve: {
'params': function () { return id }
}
}).closed.then(function(){
window.alert('Modal closed');
});
here is the plunker http://plnkr.co/edit/yB3k8e3R3ZLQFQ6sfLYW?p=preview
这是 plunker http://plnkr.co/edit/yB3k8e3R3ZLQFQ6sfLYW?p=preview
回答by Ruben.sar
Use modalInstance.result
promise second callback to catch the closing event.
I'm also getting exception 'Unable to get property 'then' of undefined or null reference'on .closed.then
,
使用modalInstance.result
promise 的第二个回调来捕捉关闭事件。我也越来越例外“无法获取财产'的未定义或空引用”,那么上.closed.then
,
var modalInstance = $uibModal.open({
templateUrl: "myModalContent.html",
controller: "termModalCtrl",
windowClass: 'app-modal-window',
resolve: {
'params': function () { return id }
}
});
modalInstance.result
.then(function (selectedItem) {
//
}, function () {
//close callback
console.info('modal closed');
});