Angular · Middle · Question 41 of 75

How do you get a reference to a child element or component in the class?

searchInput = viewChild.required<ElementRef<HTMLInputElement>>("search");
focus() { this.searchInput().nativeElement.focus(); }

Explanation

Signal queries update automatically when the element appears or disappears (e.g. inside @if). contentChild/contentChildren query projected content instead of the component's own view.

Further reading

Problem with a question

Angular · Middle · question 41

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