Template reference variables (#) memberikan akses langsung ke elemen DOM atau komponen.
Reference ke Elemen
<input #nameInput type="text" />
<button (click)="greet(nameInput.value)">Sapa</button>
@ViewChild
export class AppComponent {
@ViewChild("nameInput") inputEl!: ElementRef
focusInput() {
this.inputEl.nativeElement.focus()
}
}
Reference ke Komponen
<app-counter #counter />
<button (click)="counter.reset()">Reset Counter</button>
@ViewChild diakses setelah ngAfterViewInit lifecycle hook.