Sub topic 13
Template Reference Variables
This chapter assumes you know component anatomy, interpolation, and event binding from earlier chapters. You should also be comfortable with basic DOM element properties such as value.
A template reference variable is a name you give to something in a template so that other parts of the same template can use it. You declare it with a # prefix on an element, such as <input #query />. After that, query works like a local variable in any template expression or event handler.
What the Variable Points To
What the name refers to depends on where you declare it.
| Declared on | The variable holds |
|---|---|
| A plain HTML element | The DOM element, such as `HTMLInputElement` |
| A component's host element | The component's class instance |
| An element with a directive, using `#name="exportAsName"` | That directive's instance, such as `#f="ngForm"` |
| `<ng-template>` | A `TemplateRef` |
Reading a DOM Element
The most common use is to pass an input element to an event handler, so you don't need a binding to track every keystroke.
import { Component } from '@angular/core';
@Component({
selector: 'app-greeter',
template: `
<input #nameBox placeholder="Your name" />
<button (click)="greet(nameBox)">Greet</button>
<p>{{ greeting }}</p>
`
})
export class GreeterComponent {
greeting = '';
greet(box: HTMLInputElement) {
this.greeting = box.value ? `Hello, ${box.value}!` : 'Please type a name.';
box.value = '';
}
}Calling a Child Component
When the variable sits on a component's tag, it holds that component's instance. The parent template can then read the child's public members and call its methods.
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-stopwatch',
template: `<span>{{ seconds() }}s</span>`
})
export class StopwatchComponent {
seconds = signal(0);
tick() { this.seconds.update(s => s + 1); }
reset() { this.seconds.set(0); }
}
@Component({
selector: 'app-dashboard',
imports: [StopwatchComponent],
template: `
<app-stopwatch #watch />
<button (click)="watch.tick()">Tick</button>
<button (click)="watch.reset()">Reset</button>
`
})
export class DashboardComponent {}This works only inside the template. To reach the child from the parent's class, use a query, which the chapter on viewChild covers.
Common Mistakes
- Scope limits. A variable declared inside an
@if,@for, or<ng-template>block is visible only within that block. A variable declared outside a block is visible inside it. - Name clashes. A template variable with the same name as a class property shadows that property in the template.
- No reassignment. You cannot write
nameBox = something. You can still change the referenced object's properties, asbox.value = ''does in the greeter example.
Exercise
In the dashboard example, you move <app-stopwatch #watch /> inside an @if (visible) { ... } block but leave the two buttons outside it. What happens, and how do you fix it?
Show answer
The template fails to compile, because watch is scoped to the @if block and the buttons outside the block cannot see it. Fix it by moving the buttons inside the same @if block, or by keeping the stopwatch outside the conditional.
Exercise
What does #f hold in <form #f="ngForm">, and what would it hold without ="ngForm"?
Show answer
With ="ngForm", it holds the NgForm directive instance, which NgForm exports under that name. Without it, #f holds the plain HTMLFormElement.