AngularJS - Append element to each ng-repeat iteration inside a directive

后端 未结 1 1952
青春惊慌失措
青春惊慌失措 2021-01-03 10:26

I\'m using a ng-repeat inside a element together with a directive.

Html:


  

        
相关标签:
1条回答
  • 2021-01-03 11:12

    The reason why you don't get Angular binding inside your child is because you lack compiling it. When the link function runs, the element has already been compiled, and thus, Angular augmented. All you got to do is to $compile your content by hand. First of, don't eval your template, or you will lose your binding tips.

    app.directive('createTable', function ($compile) {
      return {
        link: function (scope, element, attrs) {
          var contentTr = angular.element('<tr ng-show=&quot;false&quot;><td>test</td></tr>');
          contentTr.insertBefore(element);
          $compile(contentTr)(scope);
        }
      }
    });
    

    Another tip: you never enclose your elements in jQuery ($). If you have jQuery in your page, all Angular elements are already a jQuery augmented element.

    Finally, the correct way to solve what you need is to use the directive compile function (read 'Compilation process, and directive matching' and 'Compile function') to modify elements before its compilation.

    As a last effort, read the entire Directive guide, this is a valuable resource.

    0 讨论(0)
提交回复
热议问题