0

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'?

bambi
  • 1,159
  • 2
  • 14
  • 31
  • 1
    Can 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
  • 1
    you 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 Answers2

0

You can prepend the ng-if directive on your element. Take in to account that ng-if directive creates a new scope.

<div your-directive ng-if="ctrl.displayDirective" > </div>
Raulucco
  • 3,406
  • 1
  • 21
  • 27
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

Community
  • 1
  • 1
harishr
  • 17,807
  • 9
  • 78
  • 125
  • 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