Full answer
A reference variable names something in the template: <input #nameInput />. Anywhere in the same template you can use nameInput to read its properties, for example nameInput.value. On a component tag, the variable refers to the component instance. On a plain element, to the DOM element.
To reach the same thing from the TypeScript class, use a view query:
viewChild<ElementRef<HTMLInputElement>>('nameInput')returns a signal that holds the element reference orundefined.viewChild.required(...)throws if the result is missing when read.viewChild(ChildComponent)finds a child by type.viewChildren(...)returns a signal of all matches.
Signal queries (stable since Angular 19) update automatically, so you can use them in computed and effect. The older decorators, @ViewChild and @ViewChildren, still work. They filled the field at a later time, and you had to wait for ngAfterViewInit.
When is the result ready? After the first render. In the constructor or ngOnInit the signal returns undefined. If the element is inside @if or @for, it appears and disappears with the block.
Use queries sparingly. Prefer data binding. A good reason is to call focus(), to scroll an element into view, or to call a method on a child.
Related: contentChild and contentChildren read items projected into <ng-content>.
import { Component, ElementRef, viewChild } from '@angular/core';
@Component({
selector: 'app-search-box',
template: `
<input #nameInput placeholder="Student name" />
<button type="button" (click)="focusInput()">Focus the field</button>
<button type="button" (click)="log(nameInput.value)">Log value</button>
`,
})
export class SearchBoxComponent {
private readonly nameInput = viewChild.required<ElementRef<HTMLInputElement>>('nameInput');
focusInput(): void {
this.nameInput().nativeElement.focus();
}
log(text: string): void {
console.log('typed:', text);
}
}Angular 20.3, strict template check passes. Clicking the button moves the cursor to the input.
Key points
- `#ref` names an element or component in a template.
- `viewChild()` and `viewChildren()` are signal queries; `@ViewChild` is the older decorator.
- Results are `undefined` until the view is rendered, and when the element is inside a false `@if`.
- Use queries for focus, scrolling, and calling child methods, not for ordinary data flow.
Common mistakes
- Reading the query in the constructor and getting `undefined`.
- Changing the DOM directly with `nativeElement.innerHTML` when a binding would do.
- Using `viewChild` for an element inside a false `@if` and not checking for `undefined`.
Follow-ups
- What is the difference between `viewChild` and `contentChild`?
- When can you safely read a query result?
- How do you read a directive instance instead of the element?
Tips
- Use the focus-on-click example. It is the most realistic use.
- Mention that you reach for data binding first, and queries only when needed.
- Know the `read` option for picking `ElementRef` or `ViewContainerRef` from a tag.
Evaluation rubric
- Explains reference variables and signal queries.
- Knows when results are available.
- Gives a reasonable use case.