【问题标题】:AngularJS recursive template renderingAngularJS 递归模板渲染
【发布时间】:2014-01-31 16:30:21
【问题描述】:

我就是无法让它工作。简化所有内容并将其放在小提琴中:http://jsfiddle.net/svdoever/94aQH/1/

我想渲染一本书的分层章节,其中包含段落,段落可以包含子段落。

代码:

angular.module("myApp", []).controller("TreeController", ['$scope', function($scope) {
$scope.vm = {};
$scope.vm.chapter = {
        "Id": "N7313",
        "Title": "1 Chapter1",
        "Content": "<p>Contents1</p>",
        "Paragraphs": [
            {
                "Id": "N1.1",
                "Title": "1.1 Paragraph1.1",
                "Content": "<p>Content2</p>",
                "Paragraphs": [
                    {
                        "Id": "N1.1.1",
                        "Title": "1.1.1 Paragraph1.1.1",
                        "Content": "<p>ContentA</p>",
                        "Paragraphs": []
                    },
                    {
                        "Id": "N1.1.2",
                        "Title": "1.1.2 Paragraph1.1.2",
                        "Content": "<p>ContentB.</p>",
                        "Paragraphs": []
                    }
                ]
            },
            {
                "Id": "N1.2",
                "Title": "1.2 Paragraph1.2",
                "Content": "<p>Content3.</p>",
                "Paragraphs": []
            }
        ]
    };
}]);

还有html:

<div ng-app="Application" ng-controller="TreeController">
  <script id="paragraphTmpl.html" type="text/ng-template">
    <a class="anchor" ng-attr-id="data.Id"></a>
    <h4 ng-bind="data.Title" />
    <div class="paragraph-text" ng-bind-html="data.Content"></div>
    <!-- Want to loose the div in the repeat as well -->
    <div ng-repeat="paragraph in data.Paragraphs" ng-include="paragraphTmpl.html"></div>
  </script>

  <div class="bookchapter">
    <a class="anchor" ng-attr-id="vm.chapter.Id"></a>
    <h3 ng-bind="vm.chapter.Title" />
    <div class="chapter-text" ng-bind-html="vm.chapter.Content"/>
    <div ng-repeat="paragraph in vm.chapter.Paragraphs" ng-include="paragraphTmpl.html"/>
  </div>
</div>

我也不希望在评论中指定的重复中呈现 div。我知道如何通过淘汰赛来做到这一点,但在 AngularJS 中找不到它。

我们将不胜感激。

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    这个小提琴几乎达到了你想要的http://jsfiddle.net/uBMKr/1/

    关键是您在 ng-include 中的模板 src 名称周围缺少 ''。这是因为没有引号,它正在寻找对范围变量的引用。

    &lt;ng-include src="mysrc"&gt;&lt;/ng-include&gt; - 寻找 $scope.mysrc

    &lt;ng-include src="'mysrc'"&gt;&lt;/ng-include&gt; - 寻找名为“mysrc”的模板

    它唯一不能做的就是删除 ng-repeat &lt;div&gt;,我认为你做不到(至少很容易)。

    【讨论】:

    • 您好 M Way,感谢您在双引号中使用单引号的提示,我错过了。但是,您的小提琴不会迭代到层次结构中。试图让它工作,但无法让它工作。
    • 抱歉,我更新了链接以更正我的错字:)
    【解决方案2】:

    如果您使用子范围来管理任意大尺寸的递归,我认为您需要使用该元素。如果data 都共享相同的父元素,您还能如何让data 引用不同的对象?

    因此,我认为您有两个选择:创建具有 compile 函数的自定义指令(我不太熟悉)或对段落进行硬编码以具有最大递归深度(使用 ngIf就像在我的 plnkr 中删除未使用的 &lt;div&gt;'s)。这是a good question to get you started。 (当然,如果您还没有阅读 Angular 在directives 上的文档会有所帮助)。

    我修复了你的代码以执行所有操作,但删除了多余的 div。

    http://plnkr.co/edit/ONnvYj91HRSvboWUxDg2

    <div ng-app="Application" ng-controller="TreeController">
      <script id="paragraphTmpl.html" type="text/ng-template">
        <a class="anchor" ng-attr-id="data.Id"></a>
        <h4 ng-bind="data.Title"></h4>
        <div class="paragraph-text" ng-bind-html="data.Content"></div>
        <!-- Want to loose the div in the repeat as well -->
        <ng-include
          ng-if="data.Paragraphs.length > 0" 
          onload="data = paragraph" 
          ng-repeat="paragraph in data.Paragraphs" 
          src="'paragraphTmpl.html'"></div>
      </script>
    
      <div class="bookchapter">
        <a class="anchor" ng-attr-id="vm.chapter.Id"></a>
        <h3 ng-bind="vm.chapter.Title"></h3>
        <div class="chapter-text" ng-bind-html="vm.chapter.Content"></div>
        <ng-include  
          ng-repeat="paragraph in vm.chapter.Paragraphs" 
          ng-if="vm.chapter.Paragraphs.length > 0"
          src="'paragraphTmpl.html'" 
          onload="data = paragraph"/>
      </div>
    </div>
    

    【讨论】:

    • 太好了,你成功了!很遗憾我必须引入额外的 div... 在淘汰赛中我使用以下方法解决了它:
    • 我忽略了它,我想要删除的 div 也不见了...您会接受,但有一个问题:为什么要对段落长度进行额外测试?如果数组为空,则 ng-repeat 已经有效。有什么(性能?)原因吗?
    • 谢谢!您是指&lt;div&gt;&lt;script&gt; 内部的ngIf 还是两者兼而有之?
    • 嗯,也许它不再需要了。如果我记得它让它没有它冻结。哦,那可能是当我拥有一个具有自定义编译功能的指令时。是的,所以不再需要了。很好的收获。
    猜你喜欢
    • 2019-11-19
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    相关资源
    最近更新 更多