AngularJS - Dynamically creating elements that specify directives

前端 未结 1 1792
梦如初夏
梦如初夏 2020-12-02 15:36

I have a setup like this:

  • Controller c broadcasts event e
  • Directive d listens for e, and on
相关标签:
1条回答
  • 2020-12-02 15:58

    See $compile. You can use this service similarly to this:

    var newDirective = angular.element('<div d2></div>');
    element.append(newDirective);
    $compile(newDirective)($scope);
    

    This will perform the compilation and linking of your new element, and set d2 into action.

    However you may find it simpler and more angular if you can somehow rewrite your original directive in terms of other built in directives like ng-repeat or ng-include that will perform the compile and link for you.

    If your directive is simple enough it could just do something along the lines of adding to an array when hearing your event, and specify a template like

    <div ng-repeat="evt in recordedEvents">
        <div d2="evt"></div>
    </div>
    
    0 讨论(0)
提交回复
热议问题