Angularjs templateUrl fails to bind attributes inside ng-repeat

后端 未结 5 1201
时光取名叫无心
时光取名叫无心 2020-12-20 01:59

I\'m using directive to display html snippets.

And templateUrl inside the directive, to be able to include snippets as html file.

The directive does not wor

5条回答
  •  别那么骄傲
    2020-12-20 02:36

    Just made changes in directive structure. Instead of rendering all templates using ng-repeat we will render it using directive itself, for that we will pass entire template array to directive.

    HTML

    Directive

    angular.module('myApp', [])
    .controller('test',function(){})
        .directive("myTemplate", function ($templateCache, $compile) {
        return {
            restrict: 'EA',
            replace: true,
            scope: {
                snippets: '='
            },
            link: function(scope, element, attrs){
                angular.forEach(scope.snippets, function(val, index){
                    //creating new element inside angularjs
                    element.append($compile($templateCache.get(val))(scope));
                });
            }
        };
    });
    

    Working Fiddle

    Hope this could help you. Thanks.

提交回复
热议问题