对角度有点陌生。是否有可能replace包含模板内容的 ng-include 节点?例如,与:
<div ng-app>
<script type="text/ng-template" id="test.html">
<p>Test</p>
</script>
<div ng-include src="'test.html'"></div>
</div>
生成的html为:
<div ng-app>
<script type="text/ng-template" id="test.html">
<p>Test</p>
</script>
<div ng-include src="'test.html'">
<span class="ng-scope"> </span>
<p>Test</p>
<span class="ng-scope"> </span>
</div>
</div>
但我想要的是:
<div ng-app>
<script type="text/ng-template" id="test.html">
<p>Test</p>
</script>
<p>Test</p>
</div>
我遇到了同样的问题,并且仍然希望 ng-include 的功能包含动态模板。我正在构建一个动态 Bootstrap 工具栏,我需要更清晰的标记才能正确应用 CSS 样式。
这是我为感兴趣的人提出的解决方案:
HTML:
<div ng-include src="dynamicTemplatePath" include-replace></div>
自定义指令:
app.directive('includeReplace', function () {
return {
require: 'ngInclude',
restrict: 'A', /* optional */
link: function (scope, el, attrs) {
el.replaceWith(el.children());
}
};
});
如果在上面的示例中使用此解决方案,则将scope.dynamicTemplatePath 设置为“test.html”将产生所需的标记。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)