谢谢你的好解决方案。我刚刚发布了一些实现代码
.directive('modal', function($templateCache, $compile) {
return function(scope, element, attrs) {
var currentModalId = attrs.modalId;
var templateHtml = $templateCache.get(attrs.template);
var modalScope, modalElement;
scope.$on('modal:open', function(event, modalId) {
if(modalId == null || currentModalId === modalId) {
openModal();
}
});
scope.$on('modal:close', function(event, modalId) {
if(modalId == null || currentModalId === modalId) {
closeModal();
}
});
function openModal() {
// always use raw template to prevent ng-repeat directive change previous layout
modalElement = $(templateHtml);
// create new inherited scope every time open modal
modalScope = scope.$new(false);
// link template to new inherited scope of modal
$compile(modalElement)(modalScope);
modalElement.on('hidden.bs.modal', function() {
if(modalScope != null) {
// destroy scope of modal every time close modal
modalScope.$destroy();
}
modalElement.remove();
});
modalElement.modal({
show: true,
backdrop: 'static'
});
}
function closeModal() {
if(modalElement != null) {
modalElement.modal('hide');
}
}
};
});