当 AngularUI Bootstrap 模态已被关闭且动画已完成执行时调用函数
calling a function when AngularUI Bootstrap modal has been dismissed and animation has finished executing
我正在使用 Angular UI bootstrap 模式,我 运行 遇到了一些问题。
我想在 bootstrap 模态关闭动画结束时调用一个函数。下面的代码块将在模态开始消失时立即调用 cancel() 函数——而不是在模态消失动画结束时调用。
Angular UI 不使用事件,因此没有 'hidden.bs.modal' 事件被触发(至少,据我所知)。
var instance = $modal.open({...});
instance.result.then(function(data) {
return success(data);
}, function() {
return cancel();
})
当模式开始关闭时,cancel() 块立即运行。当 Bootstrap 模态的 关闭动画 结束时,我需要执行代码。
如何使用 angular UI 实现此目的?
参考组件:
https://angular-ui.github.io/bootstrap/#/modal
谢谢!
有点晚了,但希望它仍然有帮助!您可以劫持 uib-modal-window 指令并检查其范围何时被销毁(它是一个独立的范围指令)。当模态最终从文档中删除时,范围被销毁。我还会使用服务来封装功能:
服务
app.service('Modals', function ($uibModal, $q) {
var service = this,
// Unique class prefix
WINDOW_CLASS_PREFIX = 'modal-window-interceptor-',
// Map to save created modal instances (key is unique class)
openedWindows = {};
this.open = function (options) {
// create unique class
var windowClass = _.uniqueId(WINDOW_CLASS_PREFIX);
// check if we already have a defined class
if (options.windowClass) {
options.windowClass += ' ' + windowClass;
} else {
options.windowClass = windowClass;
}
// create new modal instance
var instance = $uibModal.open(options);
// attach a new promise which will be resolved when the modal is removed
var removedDeferred = $q.defer();
instance.removed = removedDeferred.promise;
// remember instance in internal map
openedWindows[windowClass] = {
instance: instance,
removedDeferred: removedDeferred
};
return instance;
};
this.afterRemove = function (modalElement) {
// get the unique window class assigned to the modal
var windowClass = _.find(_.keys(openedWindows), function (windowClass) {
return modalElement.hasClass(windowClass);
});
// check if we have found a valid class
if (!windowClass || !openedWindows[windowClass]) {
return;
}
// get the deferred object, resolve and clean up
var removedDeferred = openedWindows[windowClass].removedDeferred;
removedDeferred.resolve();
delete openedWindows[windowClass];
};
return this;
});
指令
app.directive('uibModalWindow', function (Modals) {
return {
link: function (scope, element) {
scope.$on('$destroy', function () {
Modals.afterRemove(element);
});
}
}
});
并在您的控制器中使用它,如下所示:
app.controller('MainCtrl', function ($scope, Modals) {
$scope.openModal = function () {
var instance = Modals.open({
template: '<div class="modal-body">Close Me</div>' +
'<div class="modal-footer"><a class="btn btn-default" ng-click="$close()">Close</a></div>'
});
instance.result.finally(function () {
alert('result');
});
instance.removed.then(function () {
alert('closed');
});
};
});
我还写了一篇关于它的博客 post here。
我正在使用 Angular UI bootstrap 模式,我 运行 遇到了一些问题。
我想在 bootstrap 模态关闭动画结束时调用一个函数。下面的代码块将在模态开始消失时立即调用 cancel() 函数——而不是在模态消失动画结束时调用。
Angular UI 不使用事件,因此没有 'hidden.bs.modal' 事件被触发(至少,据我所知)。
var instance = $modal.open({...});
instance.result.then(function(data) {
return success(data);
}, function() {
return cancel();
})
当模式开始关闭时,cancel() 块立即运行。当 Bootstrap 模态的 关闭动画 结束时,我需要执行代码。
如何使用 angular UI 实现此目的?
参考组件:
https://angular-ui.github.io/bootstrap/#/modal
谢谢!
有点晚了,但希望它仍然有帮助!您可以劫持 uib-modal-window 指令并检查其范围何时被销毁(它是一个独立的范围指令)。当模态最终从文档中删除时,范围被销毁。我还会使用服务来封装功能:
服务
app.service('Modals', function ($uibModal, $q) {
var service = this,
// Unique class prefix
WINDOW_CLASS_PREFIX = 'modal-window-interceptor-',
// Map to save created modal instances (key is unique class)
openedWindows = {};
this.open = function (options) {
// create unique class
var windowClass = _.uniqueId(WINDOW_CLASS_PREFIX);
// check if we already have a defined class
if (options.windowClass) {
options.windowClass += ' ' + windowClass;
} else {
options.windowClass = windowClass;
}
// create new modal instance
var instance = $uibModal.open(options);
// attach a new promise which will be resolved when the modal is removed
var removedDeferred = $q.defer();
instance.removed = removedDeferred.promise;
// remember instance in internal map
openedWindows[windowClass] = {
instance: instance,
removedDeferred: removedDeferred
};
return instance;
};
this.afterRemove = function (modalElement) {
// get the unique window class assigned to the modal
var windowClass = _.find(_.keys(openedWindows), function (windowClass) {
return modalElement.hasClass(windowClass);
});
// check if we have found a valid class
if (!windowClass || !openedWindows[windowClass]) {
return;
}
// get the deferred object, resolve and clean up
var removedDeferred = openedWindows[windowClass].removedDeferred;
removedDeferred.resolve();
delete openedWindows[windowClass];
};
return this;
});
指令
app.directive('uibModalWindow', function (Modals) {
return {
link: function (scope, element) {
scope.$on('$destroy', function () {
Modals.afterRemove(element);
});
}
}
});
并在您的控制器中使用它,如下所示:
app.controller('MainCtrl', function ($scope, Modals) {
$scope.openModal = function () {
var instance = Modals.open({
template: '<div class="modal-body">Close Me</div>' +
'<div class="modal-footer"><a class="btn btn-default" ng-click="$close()">Close</a></div>'
});
instance.result.finally(function () {
alert('result');
});
instance.removed.then(function () {
alert('closed');
});
};
});
我还写了一篇关于它的博客 post here。