关闭事件后的角度 ui 模式 [英] angular ui modal after close event
问题描述
有没有办法在调用模态窗口后调用函数(无论是通过按钮还是通过单击背景发生)
Is there a way I can call a function after a modal window got called (no matter if it happened with a button or by clicking on the backdrop)
var dialog, options;
options = {
windowClass: "lightBox"
templateUrl: "url to the template",
controller: "some random controller",
scope: $scope
});
$("body").css({
'overflow': 'hidden'
});
dialog = $modal.open(options);
dialog.result.then(function() {
$("body").css({
'overflow': 'auto'
});
});
我希望每次模态窗口关闭结果中的函数.然后承诺被执行.现在它只是在我手动关闭模态时执行我的 $modalInstance.close().但是如果我点击背景,这个方法不会被调用
I want that everytime the modal windows closes the function in the result.then promise get executed. Now it just executes when i close the modal manually my $modalInstance.close(). But if i click on the backdrop this method doesn't get called
知道我该怎么做吗?
推荐答案
我将假设您使用的是 angular-ui 的 Modal 对话框.但是在进入细节之前,需要一些关于 AngularJS 中 promise 的文档.您需要知道每个 then 函数都可以接受 3 个参数,例如:
I will assume that you are using the Modal dialogs from angular-ui. But before going into the details a bit of documentation around promises in AngularJS is needed. You need to know that every then function can accept 3 parameters as such :
then(successCallback, errorCallback, notifyCallback)
- successCallback 在 promise 得到解决时执行.
- errorCallback 在 promise 被拒绝时执行.
- notifyCallback 在收到通知时执行.
- successCallback is executed when the promise is resolved.
- errorCallback is executed when the promise is rejected.
- notifyCallback is executed when notified.
在angular-ui的modal的情况下,点击背景会导致promise被拒绝.考虑到这一点,您的代码可以更改为:
In the case of angular-ui's modal, clicking on the backdrop will result in a rejected promise. With this in mind, your code could be changed to :
dialog.result.then(function () {
alert('Modal success at:' + new Date());
}, function () {
alert('Modal dismissed at: ' + new Date());
});
您可以在此处
这篇关于关闭事件后的角度 ui 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!