1.创建指令文件:ng g directive 名字
CLI 会创建 src/app/highlight.directive.ts 及相应的测试文件,并且在根模块 AppModule 中声明这个指令类。
2.ElementRef:可以在指令的构造函数中使用ElementRef来注入宿主DOM元素的引用,也就是放置appHighlight的那个元素ElementRef通过其nativeElement属性给你了“直接访问宿主DOM元素的能力
(1)引入ElementRef从angular的core中
(2)this.ref.nativeElement.style.backgroundColor = 'yellow'
3.响应用户引发的事件
(1)引入HostListener 从angular的core、中
(2)然后使用HostListener装饰器添加俩个“事件处理器”
4.使用@Input数据绑定 向 指令传值
(1)引入Input从core中
(2)然后把highlightColor属性添加到指令勒种