Modern Angular applications rely heavily on data binding, services, and components. But sometimes, you must directly access HTML elements or child components. That is where Template Reference Variables, ViewChild, and safe DOM manipulation come into play.
This article explains:
What Template Reference Variables are
How to use
ViewChildWhen (and when not) to manipulate the DOM
Real examples used in actual Angular projects
What Are Template Reference Variables?
Template Reference Variables (TRVs) allow you to get a direct reference to an HTML element or Angular component from the template.
You create a variable with the # symbol.
Example:
<input #usernameInput type="text">
<button (click)="save(usernameInput.value)">Save</button>
Here:
#usernameInputrefers to the<input>elementYou can pass its value directly into a method
TRVs with HTML Elements
Example: Accessing Input Value
<input #city type="text">
<button (click)="show(city.value)">Show</button>
Example: Access a DOM Element and Change Style
<p #para>Welcome to Angular</p>
<button (click)="highlight(para)">Highlight</button>
In the component:
highlight(el: HTMLElement) {
el.style.color = 'red';
}
TRVs with Child Components
You can also reference child components.
Parent Template:
<app-counter #counterRef></app-counter>
<button (click)="counterRef.increment()">Increment</button>
Child Component (Counter):
increment() { this.count++; }
This allows calling methods from the parent.
Using ViewChild for DOM Access in TypeScript
ViewChild is a decorator that helps you access:
HTML elements
Angular components
Directives
Template reference variables
From inside the TypeScript file.
Basic Syntax
@ViewChild('myInput') myInputElement!: ElementRef;
Template:
<input #myInput type="text">
Example: Accessing an Input Element
Template:
<input #emailInput type="text">
<button (click)="focusEmail()">Focus Input</button>
Component:
@ViewChild('emailInput') emailInput!: ElementRef;
focusEmail() {
this.emailInput.nativeElement.focus();
}
Example: Accessing a Child Component Using ViewChild
Child component:
export class TimerComponent {
start() { console.log('Timer started'); }
}
Parent template:
<app-timer></app-timer>
<button (click)="startTimer()">Start Timer</button>
Parent component:
@ViewChild(TimerComponent) timer!: TimerComponent;
startTimer() {
this.timer.start();
}
When Should You Use ViewChild?
Use ViewChild:
When you need to manipulate a specific DOM element
When you must call methods on a child component
When listening to custom component events is not enough
When you need to get initial sizes or layout values
When Should You Avoid ViewChild?
Avoid using ViewChild if:
You only need data binding (use
ngModel,@Input,@Output)You plan to rewrite everything using Angular animations
You want full control via RxJS and services
It causes too much coupling between components

Join the conversation! Your thoughts help the community grow.