Angular · Middle · Pytanie 41 z 75
Jak uzyskać w klasie referencję do elementu lub komponentu potomnego?
searchInput = viewChild.required<ElementRef<HTMLInputElement>>("search");
focus() { this.searchInput().nativeElement.focus(); }- ✓Poprawna odpowiedź: Zapytaniem sygnałowym:
viewChild()/viewChildren()(lub@ViewChildw starszym kodzie) - 2Przez
document.querySelectorw konstruktorze - 3Przez
inject(ElementRef)w komponencie potomnym
Wyjaśnienie
Zapytania sygnałowe aktualizują się automatycznie, gdy element pojawia się lub znika (np. wewnątrz @if). contentChild/contentChildren przeszukują projektowaną treść zamiast własnego widoku komponentu.