包含 ng-template 的指令中的角度嵌入(通用确认模态)

2024-07-01

您好,我在创建基于 Angular-bootstrap Modal 指令的通用确认指令时遇到了困难。

我找不到一种方法来将我的内容嵌入到用于模态构造的 ng-template 中,因为ng-transclude指令不会被评估,因为它是ng-template之后执行时加载$modal.open() :

index.html(指令插入):

<confirm-popup
    is-open="openConfirmation"
    on-confirm="onPopupConfirmed()"
    on-cancel="onPopupCanceled()"
>
Are you sure ? (modal #{{index}})

确认Popup.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>

verifyPopup.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();
                            }
                        }
                    );
                };
            }]
        };
    }
]);

如果还不够清楚,请看这个PLUNKER http://plnkr.co/edit/SAvygsN0P56u5kwBQxOC?p=preview我在哪里等着看”Are you sure ? (modal #2)“仅当点击”时open confirm modal #2“ 按钮。


ui-bootstrap 模式仅支持template or templateUrl作为指定内容的一种方式。无论内容如何检索,都会根据提供的范围进行编译和链接$modal(或者更确切地说,内部$modalStack) 服务。

所以,至少,像这样,没有办法提供嵌入。

一种解决方法是嵌入一个占位符指令,该指令将附加嵌入的 DOM - 但嵌入的 DOM,因为它来自与模态不同的位置,所以需要以某种方式移交给该占位符指令。您已经拥有了content作为注入的解析参数。我将稍微修改一下使用它 - 我将传递实际的 DOM,而不是解析后的 H​​TML。

所以,从高层次来看:

.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>

你的叉子 http://plnkr.co/edit/lmLcaN4FFH85VKjOnxfw?p=preview

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

包含 ng-template 的指令中的角度嵌入(通用确认模态) 的相关文章

  • 动态创建特定数量的输入表单元素

    我读过很多关于动态添加字段集的博客和帖子 但它们都给出了非常复杂的答案 我所要求的并没有那么复杂 我的 HTML 代码
  • 使用 Razor,如何将布尔值渲染为 JavaScript 变量?

    如何将布尔值呈现给 cshtml 文件中的 JavaScript 变量 目前这显示了一个语法错误 您可能还想尝试 isFollowing Model IsFollowing true 更好的方法是使用 isFollowing Json En
  • JavaScript 可以在重新加载后保持任何状态吗?

    有没有办法让 JavaScript 通过重新加载来保存任何变量 如果用户点击重新加载按钮 一旦页面重新加载 JavaScript 是否会保留任何状态 建议 Cookie HTML 5 网络存储https www w3schools com
  • 如何在 JavaScript 中访问 Chrome 拼写检查建议

    如何检测 JavaScript 文本区域内的拼写错误 有与此相关的事件吗 如何访问 Chrome 针对拼写错误单词的拼写检查建议 如何访问 Chrome 针对拼写错误单词的拼写检查建议 尽我所知 你不能 https stackoverflo
  • 使用 getElementById 获取锚标记的对象

    anchorobject document getElementById backbutton alert anchorobject a href http www hotmail com back a 上面的代码提醒 href 属性字符串
  • element.bind 和 element.on 有什么区别

    Folks 我一直在关注 angularjs 指令的示例 我看到有些人使用 element bind click callback 而其他人则使用 element on click callback 关于两者之间有什么区别以及何时应用哪个有
  • 在 Ionic 中使用 ElementRef 将映射加载到 ngSwitch 子范围

    我正在尝试访问ngSwitchCase查看使用 ViewChild and 元素参考在我的 Ionic 3 应用程序中加载谷歌地图 我明白了ngSwitch创建自己的范围 但无论如何都无法访问它 所以我可以将地图从谷歌加载到 map id
  • 为什么这个一次性绑定表现得好像它不存在一样?

    我似乎无法一次性绑定工作 我的实际应用程序逻辑对于 plunker 来说有点太复杂了 但我什至无法得到这样的简单场景http plnkr co edit ka57xquoR2ZdY2F0li76 http plnkr co edit ka5
  • 如何查看v8生成的机器码?

    有谁知道我如何才能看到实际的机器代码v8 http code google com p v8 从 Javascript 生成 我已经做到了Script Compile in src api cc但我不知道从那里该去哪里 我不知道如何从 C
  • 显示为问号的度unicode

    以下代码显示为问号而不是度数符号 var airF Math round Number MDTMOBILE RWISWeather i AirTemp u00B0 F tempTable find td eq 4 text airF var
  • Javascript:单击正文中除其中一个元素之外的任何位置

    我希望能够单击正文中除该特定元素之外的任何位置 我无法找出我所做的代码有什么问题 当我单击一个特定元素时 except inside body 我不想让它隐藏 但是当我点击body它应该隐藏 HTML
  • 如何在单击时和用户单击离开时关闭菜单?

    我有以下代码 function document ready function clicker class click function show menu users show jQuery clicker class click fun
  • chrome 对象 console.log 中的奇怪行为

    当我尝试在 Chrome 中 console log 一个对象时 它在 console log 的第一行中指出 您可以在其中看到 Object 此处的小摘要 我的 posts 数组的长度为 0 posts Array 0 然而 当我展开帖子
  • 画布中圆弧的不同 fillStyle 颜色

    我想这个问题的解决方案非常简单 如果这是非常明显的 请提前道歉 但我似乎无法弄清楚如何为两个不同的弧设置两个不同的 fillStyles 我只是想能够绘制不同的彩色圆圈 下面我介绍了我通常如何在画布中使用其他形状 绘图方法来完成此操作 但由
  • 'data' 为 null 或不是对象 IE8

    我正在使用 iframe 将消息传输到其父页面postMessage 这是我的代码 在 iframe 中 history date click function event window top postMessage this text
  • 通过 http 在两个 Node.js 服务器之间传输文件

    我有两个通过 http 相互通信的 node js express 服务器 server A它还与浏览器通信并可以处理文件上传请求 当文件上传到server A我想将其原样转移到server B以便进一步加工 最好的方法是什么 最好与请求
  • 为 div 标签设置属性

    我有一个简单的代码 div class content div 我想使用 javascript 回显 div 标签内的某些内容以显示这种方式 div class content div 我需要使用 javascript 因为如果屏幕宽于 9
  • 为 div 标签设置属性

    我有一个简单的代码 div class content div 我想使用 javascript 回显 div 标签内的某些内容以显示这种方式 div class content div 我需要使用 javascript 因为如果屏幕宽于 9
  • ng-model 未在 Modal 内更新

    我分配的 ng model 是控制器 可以在模态中看到 但是当我更新模态中的数据时 scope 变量没有更新 下拉菜单也不起作用 我在控制器中定义了其值 我需要更改指令吗 div class container h1 Modal examp
  • 克隆元件查找器

    在研究可用的同时ElementFinder and ElementArrayFinder方法 我注意到有一个clone method http angular github io protractor api view ElementFin

随机推荐