是否可以在指令选择器中使用冒号?

Is it possible to have colon symbol in directive selector?

是否可以在指令选择器中使用冒号?这行不通

@Directive({
  selector: '[my:selector]'
})
class MyDirective {
  constructor(el: ElementRef) {}
}

see plunker

来自Angular的Directive Documentation(在元数据属性下):

selector 必须是字符串。

selector 可以声明为以下之一:

  • element-name:select 按元素名称。
  • .class:select class 姓名。
  • [attribute]:select 按属性名称。
  • [attribute=value]:select 按属性名称和值。
  • :not(sub_selector): select 仅当元素不匹配 sub_selector.
  • selector1, selector2:select 如果 selector1 或 selector2 匹配。

请记住,您基本上是在该字符串中传递 CSS selector(有一些例外,并非每个属性 selectors 或伪 selectors 都有效) ,因此因为 [attribute:selector] 不是有效的 CSS selector,并且不是 Angular 可以理解的有效替代项之一,所以它失败了。