代码之家  ›  专栏  ›  技术社区  ›  Hutch

在Angular中为指令使用HTML标记

  •  0
  • Hutch  · 技术社区  · 8 年前

    在Angular(2+)中,通常使用HTML标记将组件添加到模板中,并使用HTML属性添加指令:在这两种情况下,这都是通过@component/@指令修饰器中的selector参数实现的:

    <!-- 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>
    

    我想知道这是否是官方支持的角度。

    1 回复  |  直到 8 年前
        1
  •  2
  •   Gurvinder Guraya    8 年前

    是的,它也可以是标签。有关详细信息,请参阅此文档。

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