Avoid using extra DOM nodes when using nginclude

后端 未结 5 765
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-31 10:23

I\'m struggling to wrap my mind around how to have an ng-include not use an extra DOM element as I\'m building an angular app from a plain-HTML demo. I\'m working with pretty sl

5条回答
  •  误落风尘
    2021-01-31 10:31

    Some of the other answers suggest replace:true, but keep in mind that replace:true in templates is marked for deprecation.

    Instead, in an answer to a similar question, we find an alternative: It allows you to write:

    Custom Directive:

    app.directive('includeReplace', function () {
        return {
            require: 'ngInclude',
            restrict: 'A', /* optional */
            link: function (scope, el, attrs) {
                el.replaceWith(el.children());
            }
        };
    });
    

    (cut'n'paste from the other answer)

提交回复
热议问题