How to dynamically remove and add directive to one element in javascript? Why it's not possible to do that with setAttribute() and removeAttribute() if in directive definition there is: restrict: 'A'?
            Asked
            
        
        
            Active
            
        
            Viewed 761 times
        
    0
            
            
        - 
                    1Can you show some code ? – Michał Lach Apr 15 '15 at 08:43
- 
                    setAttribute / removeAttribute only handles string attributes on a dom element. You are trying to manipulate angular outside of angular. I thnik there is a way to notify angular about dom changes done outside of the framewrok, but I'm not sure how its done. – mrak Apr 15 '15 at 08:49
- 
                    1you need to use $compile if u r adding or removing attribute directives – harishr Apr 15 '15 at 09:06
- 
                    @entre, can you find any example of it? – bambi Apr 15 '15 at 09:16
2 Answers
0
            
            
        to add a new directive as an attribute
angular.module('app')
  .directive('parentDirective', function ($compile) {
    return {
      restrict: 'E',
      link: function link(scope,element, attrs) {
        element.attr('child-directive', 'value');
        $compile(element)(scope);
      }
    };
  });
read more here
- 
                    Do somebody know how to do this in Angular2+? E.g. to add the core directive ‚formControlName‘ to a component instance. – Patrick Sep 04 '21 at 06:47
 
     
     
    