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(); }- ✓Correct answer: With a signal query:
viewChild()/viewChildren()(or@ViewChildin older code) - 2With
document.querySelectorin the constructor - 3With
inject(ElementRef)on the child
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.