85

有点新的角度。是否可以用包含模板的内容替换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>
4

7 回答 7

134

我遇到了同样的问题,但仍然希望 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”将产生所需的标记。

于 2014-01-03T20:57:23.437 回答
28

所以感谢@user1737909,我意识到 ng-include 不是要走的路。指令是更好的方法,也更明确。

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

App.directive('blah', function() {
    return {
        replace: true,
        restrict: 'E',  
        templateUrl: "test.html"
    };
});

在 html 中:

<blah></blah>
于 2013-05-12T01:49:19.030 回答
15

我有同样的问题,我的第 3 方 css 样式表不喜欢额外的 DOM 元素。

我的解决方案非常简单。只需将 ng-include 1 向上移动。所以而不是

<md-sidenav flex class="md-whiteframe-z3" md-component-id="left" md-is-locked-open="$media('gt-md')">
  <div ng-include="myService.template"></span>
</md-sidenav>

我只是做了:

<md-sidenav flex class="md-whiteframe-z3" md-component-id="left" md-is-locked-open="$media('gt-md')" ng-include="myService.template">
</md-sidenav>

我敢打赌这在大多数情况下都会起作用,即使从技术上讲它不是问题所在。

于 2015-02-01T21:29:17.710 回答
10

另一种选择是编写自己的简单替换/包含指令,例如

    .directive('myReplace', function () {
               return {
                   replace: true,
                   restrict: 'A',
                   templateUrl: function (iElement, iAttrs) {
                       if (!iAttrs.myReplace) throw new Error("my-replace: template url must be provided");
                       return iAttrs.myReplace;
                   }
               };
           });

然后将按如下方式使用:

<div my-replace="test.html"></div>
于 2014-08-30T22:25:51.510 回答
9

这才是替换孩子的正确方法

angular.module('common').directive('includeReplace', function () {
    return {
        require: 'ngInclude',
        restrict: 'A',
        compile: function (tElement, tAttrs) {
            tElement.replaceWith(tElement.children());
            return {
                post : angular.noop
            };
        }
    };
});
于 2015-11-03T20:02:58.607 回答
3

以下指令扩展了 ng-include 本机指令功能。

它添加了一个事件侦听器以在内容准备好并加载时替换原始元素。

以原来的方式使用它,只需添加“替换”属性:

<ng-include src="'src.html'" replace></ng-include>

或使用属性表示法:

<div ng-include="'src.html'" replace></div>

这是指令(请记住将“include-replace”模块作为依赖项包含在内):

angular.module('include-replace', []).directive('ngInclude', function () {
    return {
        priority: 1000,
        link: function($scope, $element, $attrs){

            if($attrs.replace !== undefined){
                var src = $scope.$eval($attrs.ngInclude || $attrs.src);

                var unbind = $scope.$on('$includeContentLoaded', function($event, loaded_src){
                    if(src === loaded_src){
                        $element.next().replaceWith($element.next().children());
                        unbind();
                    };
                });
            }
        }
    };
});
于 2016-05-26T14:59:07.057 回答
2

我会选择比@Brady Isom 提供的更安全的解决方案。

我更喜欢依靠onload给出的选项ng-include来确保在尝试删除模板之前已加载模板。

.directive('foo', [function () {
    return {
        restrict: 'E', //Or whatever you need
        scope: true,
        template: '<ng-include src="someTemplate.html" onload="replace()"></ng-include>',
        link: function (scope, elem) {
            scope.replace = function () {
                elem.replaceWith(elem.children());
            };
        }
    };
}])

不需要第二个指令,因为一切都在第一个指令中处理。

于 2015-07-29T12:48:06.883 回答