Replace ng-include node with template?

前端 未结 7 965
感情败类
感情败类 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:50

    Following directive extends ng-include native directive functionality.

    It adds an event listener to replace the original element when content is ready and loaded.

    Use it in the original way, just add "replace" attribute:

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

    or with attribute notation:

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

    Here is the directive (remember to include 'include-replace' module as dependency):

    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();
                        };
                    });
                }
            }
        };
    });
    
    0 讨论(0)
  • 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:

    <div ng-include src="dynamicTemplatePath" include-replace></div>
    

    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.

    0 讨论(0)
  • 2020-11-30 23:58

    So thanks to @user1737909, I've realized that ng-include is not the way to go. Directives are the better approach and more explicit.

    var App = angular.module('app', []);
    
    App.directive('blah', function() {
        return {
            replace: true,
            restrict: 'E',  
            templateUrl: "test.html"
        };
    });
    

    In html:

    <blah></blah>
    
    0 讨论(0)
  • 2020-11-30 23:58

    I would go with a safer solution than the one provided by @Brady Isom.

    I prefer to rely on the onload option given by ng-include to make sure the template is loaded before trying to remove it.

    .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());
                };
            }
        };
    }])
    

    No need for a second directive since everything is handled within the first one.

    0 讨论(0)
  • 2020-12-01 00:06

    Another alternative is to write your own simple replace/include directive e.g.

        .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;
                       }
                   };
               });
    

    This would then be used as follows:

    <div my-replace="test.html"></div>
    
    0 讨论(0)
  • 2020-12-01 00:07

    This is the correct way of replacing the children

    angular.module('common').directive('includeReplace', function () {
        return {
            require: 'ngInclude',
            restrict: 'A',
            compile: function (tElement, tAttrs) {
                tElement.replaceWith(tElement.children());
                return {
                    post : angular.noop
                };
            }
        };
    });
    
    0 讨论(0)
提交回复
热议问题