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()); }
}

Explanation

Directives are used as attributes (<input appAutofocus />). afterNextRender runs only in the browser after rendering, so the code is safe with SSR.

Further reading

Problem with a question

Angular · Middle · question 46

Test yourself

Take the Angular · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz