在 Angular 中为指令使用 HTML 标签

Using an HTML Tag for a Directive in Angular

在Angular (2+)中,通常使用HTML标签将组件添加到模板,并使用HTML属性添加指令:在这两种情况下都可以实现通过 @Component/@Directive 装饰器中的选择器参数:

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

我测试了是否可以设置一个使用 HTML 标记而不是属性的指令,它似乎工作正常:

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

我只想知道 Angular 是否正式支持此功能。

是的,它可以是标签,也可以查看此文档了解更多信息

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