A directive is a class that adds behavior to elements. Types: (1) Component (a directive with a template). (2) Structural directives change layout by adding/removing elements, e.g., `*ngIf`, `*ngFor`. (3) Attribute directives change look/behavior, e.g., custom hover highlight. Use `@Directive` to build your own.
Example code
import { Directive, ElementRef, Renderer2, HostListener, Input } from '@angular/core';
@Directive({ selector: '[appHoverBackground]' })
export class HoverBackgroundDirective {
@Input('appHoverBackground') hoverColor?: string;
constructor(private el: ElementRef, private r: Renderer2) {}
@HostListener('mouseenter') onIn(){ this.r.setStyle(this.el.nativeElement, 'backgroundColor', this.hoverColor || 'yellow'); }
@HostListener('mouseleave') onOut(){ this.r.removeStyle(this.el.nativeElement, 'backgroundColor'); }
}