javascript-包含ng-template的指令中的角度转换(通用确认模态)
作者:互联网
您好,我在基于angular-bootstrap Modal指令创建通用确认指令时遇到了麻烦.
我找不到在模态构造中使用的ng-template中包含内容的方法,因为ng-transclude指令未评估,因为它是执行$modal.open()之后加载的ng-template的一部分:
index.html(定向插入):
<confirm-popup
is-open="openConfirmation"
on-confirm="onPopupConfirmed()"
on-cancel="onPopupCanceled()"
>
Are you sure ? (modal #{{index}})
ConfirmPopup.html(指令模板):
<script type="text/ng-template" id="confirmModalTemplate.html">
<div>
<div class="modal-header">
<h3>Confirm ?</h3>
</div>
<div class="modal-body">
{{directiveTranscludedContent}} // ng-transclude do not work here
</div>
<div class="modal-footer">
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
<button class="btn btn-primary" ng-click="ok()">Validate</button>
</div>
</div>
</script>
ConfirmPopup.js(指令JS):
.directive('confirmPopup', [
function() {
return {
templateUrl: 'confirmPopup.html',
restrict: 'EA',
replace: true,
transclude: true,
scope: {
isOpen: '=',
confirm: "&onConfirm",
cancel: "&onCancel"
},
controller: ['$scope', '$element', '$modal', '$transclude', '$compile', function($scope, $element, $modal, $transclude, $compile) {
// watching isOpen attribute to dispay modal when needed
$scope.$watch(
function() {
return $scope.isOpen;
},
function(newValue) {
if (newValue === true) {
openModal();
} else {
// if a modal is already dispayed : the modal must be canceled/confirmed by the user
// else (if no modal is dispayed), then do nothing
}
}
);
// open modal function
// create / register ok/cancel callbacks
// and open modal
// all on one shot
function openModal() {
$modal.open({
templateUrl: 'confirmModalTemplate.html',
controller: ['$scope', '$modalInstance', 'content', function($scope, $modalInstance, content) {
$scope.directiveTranscludedContent = content;
$scope.ok = function() {
$modalInstance.close();
};
$scope.cancel = function() {
$modalInstance.dismiss();
};
}],
resolve: {
content: function() {
return $transclude().html();
//return $compile($transclude().contents())($scope);
},
}
})
.result.then(
// modal has been validated
function() {
$scope.confirm();
},
// modal has been dismissed
function() {
if ($scope.cancel) {
$scope.cancel();
}
}
);
};
}]
};
}
]);
如果还不够清楚,请仅在单击“打开确认模式2”按钮时,在此PLUNKER上看到“您确定吗?(模式2)”.
解决方法:
ui-bootstrap模态仅支持template或templateUrl作为指定内容的方式.无论内容如何被检索,它都会通过$modal(或内部$modalStack)服务与提供的范围进行编译和链接.
因此,至少像那样,没有办法提供包含.
一种解决方法是嵌入一个占位符指令,该指令将附加被包含在DOM中的DOM,但是被包含在DOM中的DOM由于来自与模式不同的位置,因此需要以某种方式移交给该占位符.您已经将内容作为注入的resolve参数.我将在稍作修改的情况下使用它-我将传递实际的DOM,而不是经过解析的HTML.
因此,从高层次看:
.directive("confirmPopupTransclude", function($parse){
return {
link: function(scope, element, attrs){
// could have been done with "=" and isolate scope,
// but avoids an unnecessary $watch
var templateAttr = attrs.confirmPopupTransclude;
var actualTemplateDOM = $parse(templateAttr)(scope);
element.append(actualTemplateDOM);
}
};
})
并且,在openModal函数中(忽略不相关的属性):
function openModal{
$modal.open({
controller: function($scope, content){
$scope.template = content;
// etc...
},
resolve: {
content: function(){
var transcludedContent;
$transclude(function(clone){
transcludedContent = clone;
});
return transcludedContent; // actual linked DOM
},
// etc...
}
最后,在模态的实际模板中:
<div class="modal-body">
<div confirm-popup-transclude="template"></div>
</div>
标签:angular-bootstrap,transclusion,angularjs,modal-dialog,javascript 来源: https://codeday.me/bug/20191120/2044878.html