How do you build a custom attribute directive?

Angular directives, pipes and DI · Freshers and experienced

Model answer

Use @Directive with a selector in square brackets, such as [appHighlight]. Inject ElementRef to reach the host element, and use host bindings or listeners to react: in the decorator's host option, 'mouseenter' can call a method that changes the background colour.

Inputs let you configure it: [appHighlight]="'yellow'". Directives are good for reusable behaviour such as autofocus, click-outside, or tooltips, without changing the component.

Learn this topic

Angular Tutorial: Beginner to Advanced .

Sign in to add your own answer.

More questions