Replace ng-include node with template?

前端 未结 7 985
感情败类
感情败类 2020-11-30 23:36

Kinda new to angular. Is it possible to replace the ng-include node with the contents of the included template? For example, with:

7条回答
  •  遥遥无期
    2020-11-30 23:57

    I had this same issue and still wanted the features of ng-include to include a dynamic template. I was building a dynamic Bootstrap toolbar and I needed the cleaner markup for the CSS styles to be applied properly.

    Here is the solution that I came up with for those who are interested:

    HTML:

    Custom Directive:

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

    If this solution were used in the example above, setting scope.dynamicTemplatePath to 'test.html' would result in the desired markup.

提交回复
热议问题