Search code examples
angularangular-directive

Using an HTML Tag for a Directive in Angular


In Angular (2+), typically components would be added to templates using HTML tags, and directives would be added using HTML attributes: in both cases this is achieved through the selector argument within the @Component/@Directive decorator:

<!-- component set up using-- selector: 'my-component' -->
<my-component></my-component>
<!-- directive set up using-- selector: '[myDirective]' -->
<div myDirective></div>

I tested to see if I could set up a directive that will use an HTML tag instead of attribute, and it seemed to work fine:

<!-- directive set up using-- selector: 'my-tag-directive' -->
<my-tag-directive></my-tag-directive>

I would just like to know if this is officially supported in Angular.


Solution

  • Yes it could be tag as well see this documentation for more info

    https://angular.io/api/core/Directive#selector