Angular Components Created: 06 Jan 2026 Updated: 06 Jan 2026

Template Reference Variables

In modern Angular development, Template Reference Variables are a powerful way to "reach out and touch" an element or a component directly within your HTML. Think of them as a way to create a handle or a nickname for an element so you can use its properties elsewhere.

We will explain this using a simple Product Search example.

1. What is a Template Reference Variable?

A template reference variable is a name you give to an element using the hash symbol (#).

  1. Syntax: #variableName
  2. Scope: It is available anywhere within the same HTML template.

2. Basic Example: Accessing an HTML Element

Let’s say you have an input field where users type a product name. Instead of using complex data binding, you can use a reference variable to "grab" the value directly.

src/app/search/search.component.html

<div class="search-box">
<input #productInput type="text" placeholder="Enter product name...">

<button (click)="onSearch(productInput.value)">Search</button>

<p>You are searching for: {{ productInput.value }}</p>
</div>

src/app/search/search.component.ts

import { Component } from '@angular/core';

@Component({
selector: 'app-search',
standalone: true,
templateUrl: './search.component.html'
})
export class SearchComponent {
onSearch(term: string) {
console.log('Searching for:', term);
}
}

Why use this?

In the example above, we didn't need to create a variable in the TypeScript file just to hold the input text. The #productInput variable allowed the button to "peek" inside the input and grab its text instantly.

3. Advanced Example: Accessing a Child Component

You can also place a reference variable on a custom Angular component. This gives you access to that component's Public API (all its public properties and methods).

The Child Component (ProductDetail)

src/app/product-detail/product-detail.component.ts

@Component({
selector: 'app-product-detail',
standalone: true,
template: `<div class="box">Product Code: {{ code }}</div>`
})
export class ProductDetailComponent {
// Public property
code: string = 'KB-99';

// Public method
refresh() {
console.log('Product refreshed!');
}
}

The Parent Template (ProductList)

Using the # symbol on the child tag allows the parent to control the child directly from the HTML.

src/app/product-list/product-list.component.html

<app-product-detail #detail></app-product-detail>

<div class="actions">
<p>Parent is watching ID: {{ detail.code }}</p>

<button (click)="detail.refresh()">Force Refresh Child</button>
</div>

4. Key Takeaways

FeatureDescription
IdentifierAlways starts with a # (e.g., #myRef).
What it refers toThe DOM element or the Component instance it is placed on.
UsageBest for simple UI logic, accessing third-party components, or grabbing input values.
LimitationIt is only visible in the HTML template (unless you use @ViewChild in TypeScript).

When to use this approach?

  1. Simplicity: When you want to pass an input value to a function without setting up ngModel or Reactive Forms.
  2. Direct Control: When you need to call a specific method inside a child component (like play() on a video player component or open() on a modal).


Share this lesson: