当angular给我们提供的指令不够用的时候,不能满足我们需求的时候就需要自定义指令了。
ng generate directive highlightimport { Directive, ElementRef } from '@angular/core';@Directive({selector: '[appHighlight]'})export class HighlightDirective {//el: 绑定指令的那个页面元素constructor(el: ElementRef) {el.nativeElement.style.backgroundColor = 'yellow';}}
使用自定义指令:
<p appHighlight>Highlight me!</p>
