有點晚,但希望它仍然有幫助!你可以劫持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');
});
};
});
我還寫了一篇關於它的博客文章here。
我使用設置爲動畫完成所需時間的超時值對其進行了入侵。我完全不喜歡這個想法,因爲如果模態關閉的時間有變化,那麼這個超時也需要更新。這是一個非常整潔的解決方案!我會試一試。非常感謝! – LennieCodes
我只想說UI-Bootstrap的下一個版本允許你做這樣的事情:'instance.closed.then(function(){...}'我遇到的問題是下一個版本angular-UI-Bootstrap不支持angular 1.3,這會關閉我的項目。 – LennieCodes