Template Reference Variables
Angular Template Reference Variables provide a convenient way to create a reference to an HTML element, component, or directive inside an Angular template.
What is a Template Reference Variable?
A template reference variable is a variable created directly inside an Angular template using the # symbol.
It provides a reference to an element or directive instance and allows other parts of the same template to access it.
Simple Definition
A Template Reference Variable gives a name to an element so that the template can access that element and its properties.
Template Reference Variable Flow
Template Reference Variable Syntax
Input Reference
Creates a reference to an input element.
#studentInput
Read Value
Reads the current value of an input.
studentInput.value
Use in Event
Passes the reference value to a method.
(click)="showName(studentInput.value)"
Display Data
Displays the referenced value in the template.
{{ studentInput.value }}
1. Creating a Template Reference Variable
A template reference variable is created by adding the # symbol followed by a variable name.
<input #studentInput placeholder="Enter student name">
How it works
The #studentInput variable creates a reference to
the input element. The template can then access properties of
that input using studentInput.
2. Accessing Input Value
Once a reference variable is created, the template can access the current value of the referenced input element.
<input #studentInput placeholder="Enter student name">
<p>
Student Name:
{{ studentInput.value }}
</p>
If the user enters Sam:
Student Name: Sam
3. Using Reference Variable with Button
A template reference variable can be used inside an event statement to pass the current input value to a method.
export class StudentComponent {
message = '';
showName(name: string) {
this.message = 'Welcome ' + name;
}
}
<input #studentInput placeholder="Enter student name">
<button (click)="showName(studentInput.value)">
Show Name
</button>
<p>
{{ message }}
</p>
Enter: Sam
After clicking the button: Welcome Sam
4. Multiple Template Reference Variables
Multiple elements in the same template can have their own reference variables.
<input #studentName placeholder="Student Name">
<input #studentCourse placeholder="Course">
<button
(click)="
showStudent(
studentName.value,
studentCourse.value
)
">
Show Student
</button>
How it works
The template creates two references:
#studentName and
#studentCourse.
Their values can be passed to a component method.
5. Template Reference Variable with Form
Template reference variables are also commonly used with forms and form controls.
<form #studentForm="ngForm">
<input
name="studentName"
ngModel
placeholder="Enter student name">
<button
type="submit"
[disabled]="studentForm.invalid">
Submit
</button>
</form>
Important
When a reference variable is assigned to a directive such as
ngForm, the reference can provide access to the
directive instance and its properties.
6. Referencing an Angular Component
A template reference variable can also be used to reference an Angular component instance.
<app-student #studentComponent>
</app-student>
<button (click)="studentComponent.showStudent()">
Show Student
</button>
How it works
The #studentComponent reference points to the
component instance, allowing the template to access its
available public members.
Template Reference Variable vs Component Property
| Feature | Template Reference Variable | Component Property |
|---|---|---|
| Created Where? | Inside template | Inside component class |
| Syntax | #studentInput |
studentName = 'Sam' |
| Purpose | Reference an element or directive | Store application data |
| Access | Template | Component and template |
Common Template Reference Examples
Input Element
<input #studentInput>
Read Value
studentInput.value
Button Event
(click)="show(studentInput.value)"
Form Reference
<form #studentForm="ngForm">
Complete Template Reference Variable Example
export class StudentComponent {
message = '';
showStudent(name: string) {
this.message = 'Welcome ' + name;
}
}
<h2>
Student Information
</h2>
<input
#studentInput
placeholder="Enter student name">
<button
(click)="showStudent(studentInput.value)">
Show Student
</button>
<p>
{{ message }}
</p>
Student Information
Enter: Sam
Welcome Sam
Template Reference Variable Best Practices
- Use meaningful names for template reference variables.
- Use reference variables when template-level access is required.
- Avoid creating unnecessary references.
- Use component properties for application-level data.
- Keep template logic simple and readable.
- Use reference variables carefully when working with forms and component instances.
Summary
Angular Template Reference Variables provide a convenient way to reference HTML elements, directives, forms, and component instances directly inside a template.
They are created using the # symbol and can be used to access values and call available functionality from the referenced object.
Template reference variables are especially useful for input elements, forms, event handling and component references.