Skip to content
tezvyn:

Angular Template Reference Variables: A #handle for Elements

Source: angular.devMediumHow cards are made

Angular Template Reference Variables: A #handle for Elements

A template reference variable is a handle to a DOM element or component within your template. Use the #varName syntax to grab an input's value or call a child component's method. Its scope is limited to the template where it's defined.

Why it exists

Sometimes you need to perform simple interactions within a template without the overhead of creating component properties and event handlers. For example, reading a value from one element when another is clicked. Template reference variables provide a direct, lightweight way to achieve this entirely within the template's HTML.

The mental model

Think of a template reference variable as a temporary name tag you stick on an HTML element, component, or directive. This tag, created with a '#' prefix (like #phoneInput), lets other elements in the same template find and interact with the tagged element directly. It's like giving a person a nametag at a party so you can easily call them out from across the room without needing to know them beforehand.

How it works

You declare a variable by adding a hash-prefixed attribute to an element, such as <input #myInput>. Angular then makes myInput available throughout that template. By default, the variable references the DOM element itself. If you place it on an Angular component, it references the component instance, giving you access to its public methods and properties. You can then pass this variable to event handlers, like (click)="logValue(myInput.value)". The scope of this variable is strictly limited to the template where it is defined.

When to use it

Use it for simple, template-local interactions. A classic case is grabbing the value from an input field when a button is clicked, without needing a full ngModel two-way binding. It's also great for calling a public method on a child component from the parent's template, for example, (click)="childComponent.reset()".

When not to use it

Avoid using template reference variables when you need to access the element or component from your component's TypeScript class logic. They are designed for template-to-template communication. For accessing template elements from your class, the correct tool is the @ViewChild or @ViewChildren decorator. Also, don't confuse them with @let variables, which are for storing the result of an expression, not for referencing elements.

One canonical example

To get a user's name from an input field and display it in an alert without involving the component class, you can write: <input #nameInput placeholder="Enter your name"> <button (click)="alert(nameInput.value)">Greet</button> Here, #nameInput creates a reference to the input element. The button's click handler can then directly access its value property.

Interview question

Which task is best accomplished using an Angular template reference variable?

  • a.Creating a variable that can be accessed by any component in the application.
  • b.Storing the result of a calculation for repeated use in the template.
  • c.Allowing one template element to interact directly with another element's properties or methods.Correct
  • d.Obtaining a reference to a DOM element for manipulation within the component's class.
Why?

Template reference variables are designed for direct, template-local interactions, enabling one element to access another's properties or methods. Accessing elements from the component's TypeScript class requires @ViewChild, not template reference variables.

Just read this? Test yourself on what you have been reading.

Read the original → angular.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on angular — each one lists the topics its interview covers.

See open roles