Template reference variables let you easily access elements or components inside your Angular template. They help you interact with parts of your page without extra code.
Template reference variables in Angular
<element #variableName></element>
The #variableName creates a reference to the element or component.
You can use this variable elsewhere in the template to access the element or component's properties and methods.
<input #myInput type="text"> <button (click)="alert(myInput.value)">Show Value</button>
<child-component #childRef></child-component>
<button (click)="childRef.doSomething()">Call Child Method</button><input #inputRef type="text"> <button (click)="inputRef.focus()">Focus Input</button>
This component uses a template reference variable #nameInput to get the input value when the button is clicked. It then shows a greeting message below.
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` <h1>Template Reference Variable Demo</h1> <input #nameInput type="text" placeholder="Enter your name"> <button (click)="greet(nameInput.value)">Greet</button> <p>{{ message }}</p> ` }) export class AppComponent { message = ''; greet(name: string) { this.message = `Hello, ${name}!`; } }
Template reference variables only work inside the template where they are declared.
They do not create variables in the component class, only in the template.
You can reference native HTML elements or Angular components.
Template reference variables let you access elements or components directly in your template.
They are useful for reading values, calling methods, or manipulating elements without extra code in the component.
Use #variableName syntax to create them and then use the variable in your template expressions.
Practice
template reference variable in Angular templates?Solution
Step 1: Understand what template reference variables do
They let you refer to elements or components inside the template without extra code in the component class.Step 2: Identify the correct purpose
Accessing DOM elements or component instances directly in the template is exactly what template reference variables do.Final Answer:
To access a DOM element or component instance directly in the template -> Option CQuick Check:
Template reference variable = direct template access [OK]
- Confusing template reference variables with component or service creation
- Thinking they are used for styling or CSS
- Assuming they are for dependency injection
inputBox on an <input> element?Solution
Step 1: Recall Angular template reference variable syntax
Angular uses the#variableNamesyntax to declare template reference variables.Step 2: Match the syntax to the options
Only uses#inputBoxwhich is correct.Final Answer:
<input #inputBox> -> Option AQuick Check:
#variableName syntax = correct declaration [OK]
- Using ref, var, or let which are not Angular template reference syntax
- Confusing Angular syntax with other frameworks
- Missing the # symbol
<input #myInput type="text"> <button (click)="alert(myInput.value)">Show Value</button>
What happens when the button is clicked after typing "Hello" in the input?
Solution
Step 1: Understand the template reference variable usage
The#myInputvariable refers to the input element. AccessingmyInput.valuegets the current text typed.Step 2: Analyze the button click event
Clicking the button callsalert(myInput.value), so it shows the input's current value.Final Answer:
An alert box shows the text "Hello" -> Option AQuick Check:
myInput.value alerts typed text [OK]
- Thinking myInput is a string instead of element reference
- Assuming myInput is undefined or inaccessible
- Expecting no action or error on click
<input #userInput type="text"> <button (click)="logValue()">Log</button>
And component code:
logValue() {
console.log(userInput.value);
}What is the problem here?
Solution
Step 1: Identify scope of template reference variables
Template reference variables exist only in the template, not in the component class.Step 2: Check component method usage
The method tries to accessuserInputdirectly, which is undefined in the class, causing an error.Final Answer:
userInput is not defined in the component class, causing an error -> Option BQuick Check:
Template refs not accessible in class directly [OK]
- Trying to use template refs inside component class directly
- Thinking template refs need $ or other symbols
- Assuming arrow functions fix scope issues
<input #myInput type="text"> <button (click)="???">Reset</button>
Which is the correct replacement for
????Solution
Step 1: Understand template reference variable usage in event binding
You can directly access the element via the variable and set its properties in the template expression.Step 2: Choose the correct syntax to clear input value
SettingmyInput.value = ''clears the input. Usingthisor calling reset() method is incorrect here.Final Answer:
myInput.value = '' -> Option DQuick Check:
Directly assign empty string to .value to reset [OK]
- Using this.myInput which is undefined in template
- Calling reset() which input element does not have
- Trying to call a function reset() without defining it
Ready to prove it?
Pass the test and this pattern moves unknown
