【发布时间】:2013-07-09 11:42:14
【问题描述】:
我正在努力思考如何让 ng-include 不使用额外的 DOM 元素,因为我正在从纯 HTML 演示构建角度应用程序。我正在使用非常纤薄的 HTML 和完全开发的、紧密 DOM 耦合的 CSS(由 SASS 构建),并且我想不惜一切代价避免重构。
这是实际代码:
<div id="wrapper">
<header
ng-controller="HeaderController"
data-ng-class="headerType"
data-ng-include="'/templates/base/header.html'">
</header>
<section
ng-controller="SubheaderController"
data-ng-class="subheaderClass"
ng-repeat="subheader in subheaders"
data-ng-include="'/templates/base/subheader.html'">
</section>
<div
class="main"
data-ng-class="mainClass"
data-ng-view>
</div>
</div>
我需要 是一个重复的元素,但有自己的逻辑和不同的内容。内容和重复次数都取决于业务逻辑。如您所见,将 ng-controller 和 ng-repeat 放在 元素上是行不通的。然而,插入一个新的 DOM 节点,这是我试图避免的。
我错过了什么?这是最佳做法还是有更好的方法?
编辑:只是按照 cmets 的要求澄清,我要生成的最终 HTML 将是:
<div id="wrapper">
<header>...</header>
<section class="submenuX">
some content from controller A and template B (e.g. <ul>...</ul>)
</section>
<section class="submenuY">
different content from same controller A and template B (e.g. <div>...</div>)
</section>
<section class="submenuZ">
... (number of repetitions is defined in controller A e.g. through some service)
</section>
<div>...</div>
</div>
我想使用相同的模板 B (subheader.html) 的原因是为了代码简洁。我认为 subheader.html 有某种 ng-switch 以返回动态内容。
但基本上,潜在的问题是:有没有办法在不使用 DOM 节点的情况下透明地包含模板的内容?
EDIT2:解决方案需要可重复使用。 =)
【问题讨论】:
-
您能否为变体添加一个示例,现在确定您要的是什么?
-
对不起。编辑澄清。
-
您可以使用 ng-include 作为标签
<ng-include src='url'></ng-include>,而不是发出url内容以外的标签。 -
对,但我试图避免触摸与 DOM 树(带有层次结构选择器)紧密耦合的样式表,并且使用 nginclude 会迫使我这样做(因为生成的包含模板变成了ng-include 的子节点)。
标签: angularjs angularjs-ng-repeat angularjs-ng-include