加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 用模板替换ng-include节点?

发布时间:2020-12-17 09:07:38 所属栏目:安全 来源:网络整理
导读:金达新到角。是否可以用所包含的模板的内容替换ng-include节点?例如,使用: div ng-app script type="text/ng-template" id="test.html" pTest/p /script div ng-include src="'test.html'"/div/div 生成的html是: div ng-app script type="text/ng-templ
金达新到角。是否可以用所包含的模板的内容替换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”将导致所需的标记。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读