Insert directive programmatically angular

后端 未结 1 1007
别那么骄傲
别那么骄傲 2020-12-13 06:15

So I basically want to be able to trigger an event and then have a directive compile and insert its self to a position in the DOM. Currently I have something like this

相关标签:
1条回答
  • 2020-12-13 07:04

    You have to create the dom element first, then compile it and add it to the document. Something like this:

    $scope.$on('insertItem',function(ev,attrs){
      var chart = angular.element(document.createElement('chart'));
      var el = $compile( chart )( $scope );
    
      //where do you want to place the new element?
      angular.element(document.body).append(chart);
    
      $scope.insertHere = el;
    };
    

    I've created a simple example here: http://plnkr.co/edit/n7SZpyeQ9nbjVSYA7ibB?p=preview

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