9

Is there any way to add a directive to an ElementRef from TypeScript? (or to fool Angular that there is a directive added)?

Roy
  • 7,811
  • 4
  • 24
  • 47
  • I recently answered similar question [here](https://stackoverflow.com/a/52388884/5011810). I think it's your case. – Chiffie Sep 18 '18 at 15:13
  • @chiffre I don't think so – Fedor Losev Oct 03 '18 at 02:42
  • This has been answered already https://stackoverflow.com/a/39565420/11719787, Simple answer is you can't – Sameer Jan 16 '20 at 03:48
  • Does this answer your question? [How to instantiate and apply directives programmatically?](https://stackoverflow.com/questions/39563547/how-to-instantiate-and-apply-directives-programmatically) – satanTime Feb 27 '21 at 21:00

1 Answers1

0

ElementRef actually doesn't reference an Angular component. Instead, it references a HTML DOM element like HTMLDivElement. They aren't real Angular elements, so directives can't be added to them.

Eric Xue
  • 117
  • 1
  • 9