AngularJS + 指令 : Multiple Transcluded Elements

标签 angularjs angularjs-directive angularjs-ng-transclude

我正在尝试添加一个包含左面板和右面板的正文指令。但在这些面板之间,我会有一些 body 指令私有(private)的内容。我目前正在使用 嵌入=真 加载内容的选项。但是,我正在寻找一种使用两个 ng-transclude 的方法。我研究了很多如何解决这个问题,但我找不到一个优雅的解决方案。我不得不在 body 指令的编译步骤中手动添加嵌入的对象。以下是我解决该问题的方法:

body 指令

var myApp = angular.module('myApp', []);

myApp.directive('body', function () {
    return {
        restrict: 'A',
        transclude: true,
        scope: {
            title: '@'
        },
        templateUrl: 'body.html',
        compile: function compile(element, attrs, transclude) {
            return function (scope) {
                transclude(scope.$parent, function (clone) {
                    for (var i = 0; i < clone.length; i++){ 
                       var el = $(clone[i]);
                       if(el.attr('panel-left') !== undefined) {
                            element.find('#firstPanel').html(el);
                       } else if(el.attr('panel-right') !== undefined) {
                            element.find('#secondPanel').html(el);
                       }
                    }
                });
            }
        }
    };
});

myApp.directive('panelLeft', function () {
    return {
        require: "^body",
        restrict: 'A',
        transclude: true,
        replace: true,
        template: '<div ng-transclude></div>'
    };
});

myApp.directive('panelRight', function () {
    return {
        require: "^body",
        restrict: 'A',
        transclude: true,
        replace: true,
        template: '<div ng-transclude></div>'
    };
});

模板
<script type="text/ng-template" id="body.html">
    <h1> {{title}} </h1>
    <hr/>

    <div id="firstPanel" class="inner-panel"></div>
    <div id="innerMiddleContent" class="inner-panel">middle private content here</div>
    <div id="secondPanel" class="inner-panel"></div>
</script>

<div body title="Sample Body Directive">
    <div panel-left>
        Public content that goes on the left
    </div>   

    <div panel-right>
        Public content that goes on the right
    </div>    
</div>

Here是这个例子的 JSFiddle。我正在寻找这样的东西:

必备模板
 <script type="text/ng-template" id="body.html">
     <h1> {{title}} </h1>
     <hr/>

     <div id="firstPanel" class="inner-panel" ng-transclude="panel-left"></div>
     <div id="innerMiddleContent" class="inner-panel">middle private content here</div>
     <div id="secondPanel" class="inner-panel" ng-transclude="panel-right"></div>
 </script>

问题 : 难道我做错了什么?有没有推荐的方法来解决这个问题?

最佳答案

你见过这个指令吗?

https://github.com/zachsnow/ng-multi-transclude

我认为这正是您正在寻找的。

因此,对您的模板稍作修改

<div ng-multi-transclude="panel-left" class="inner-panel"></div>
<div ng-multi-transclude="panel-right" class="inner-panel">middle private content here</div>
<div id="secondPanel" class="inner-panel"></div>

然后你可以像这样使用它
<div body title="Sample Body Directive">
  <div name="panel-left">
    Public content that goes on the left
  </div>   

  <div name="panel-right">
    Public content that goes on the right
  </div>    
</div>

关于AngularJS + 指令 : Multiple Transcluded Elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25293393/

相关文章:

angularjs - ng-if 导致链接显示一秒钟然后消失。如何防止显示

angularjs - 使用 ng-show 在单击时展开嵌套列表

javascript - Angularjs 自定义指令传递对象数据

angularjs - 在 ngInclude 中使用 ngTransclude

javascript - 在模板中非法使用 ng-Transclude 指令

javascript - 在加载指令模板之前加载 ng-transinclude 内容的指令

javascript - 我需要哪个 Bower 包才能在 angularJS 中使用 locationProvider

asp.net-mvc - Angularjs 中的每个 View 是否应该有一个 Controller ?

javascript - 等待根据指令发起的 promise 解决的最佳方式是什么?

angularjs - 在指令中包装 md-tabs 会产生 "Orphan ngTransclude Directive"错误