Angular · Middle · Question 46 of 75
What is an attribute directive?
@Directive({ selector: "[appAutofocus]" })
export class Autofocus {
private el = inject(ElementRef);
constructor() { afterNextRender(() => this.el.nativeElement.focus()); }
}- ✓Correct answer: A class that adds behavior to an existing element without its own template
- 2A component that renders an attribute value
- 3A directive that can only change CSS classes
Explanation
Directives are used as attributes (<input appAutofocus />). afterNextRender runs only in the browser after rendering, so the code is safe with SSR.