javascript-包含ng-template的指令中的角度转换(通用确认模态)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-包含ng-template的指令中的角度转换(通用确认模态),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3453字,纯文字阅读大概需要5分钟。
内容图文
![javascript-包含ng-template的指令中的角度转换(通用确认模态)](/upload/InfoBanner/zyjiaocheng/662/ccc3a454f84847b8a9a4d4ea40cb04f5.jpg)
您好,我在基于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>
内容总结
以上是互联网集市为您收集整理的javascript-包含ng-template的指令中的角度转换(通用确认模态)全部内容,希望文章能够帮你解决javascript-包含ng-template的指令中的角度转换(通用确认模态)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。