directive not working inside that is ng-repeat bound

前端 未结 2 1823
小蘑菇
小蘑菇 2021-01-02 04:42

I have a table where the rows are repeated via ng-repeat. I am trying to create a template that generates columns for each row <

2条回答
  •  醉话见心
    2021-01-02 05:11

    As Pavlo wrote, you can move the tr element to the template for the directive. Another option is to use a td element and directive that replaces your td with the template that you want to use.

    Directive replaceMe

    .directive("replaceMe", ["$compile", '$http', '$templateCache', function ($compile, $http, $templateCache) {
            return {
                restrict: 'A',
                scope: {
                   position: "="
                },
                link: function (scope, element, attrs) {
                    function getTemplate(template) {
                        $http.get(template, {cache: $templateCache}).success(function (templateContent) {
                            element.replaceWith($compile(templateContent)(scope));
                        });
                    }
                    scope.$watch(attrs.template, function () {
                        if (attrs.template) {
                            getTemplate(attrs.template);
                        }
                    });
    
    
                }
            }
        }]);
    

    mytemplate.html

    {{position.Name}}
    {{position.Code}}
    
    

    plunker

提交回复
热议问题