属性型指令,应用在宿主元素时不需要
中括号:

效果如下图黄色高亮区域所示:

从外界传值给 Angular attribute Directive:

这里需要给 highlight Directive 创建一个同名 @Input 属性:

或者我们可以任意命名这个属性名称,但是 @Input 的输入参数,必须指定成和 Directive 名称一样,如下图所示,A 和 B 的值必须相同。

消费代码:

如果把 appHighLight 绑定给 browser-faker Component 的某个属性,则必须加上带有中括号的属性绑定语法了:

注意如果一切正确,在 Visual Studio Code 里,鼠标 hover 到 appHighlight 上,会有正确的类型提示:

更多Jerry的原创文章,尽在:"汪子熙":

网友评论