Template references let you easily get a part of your webpage in your code. This helps you change or read that part directly.
Template reference for direct access in Angular
<element #refName></element>
The #refName creates a name you can use inside the template.
You can use this name to get the element or component directly in your template expressions.
#myInput to focus the input box when the button is clicked.<input #myInput type="text"> <button (click)="myInput.focus()">Focus Input</button>
#childRef references a child component to call its method directly from the template.<child-component #childRef></child-component>
<button (click)="childRef.someMethod()">Call Child Method</button><input #userName type="text">
<p>You typed: {{ userName.value }}</p>This component uses a template reference #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: ` <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 references only work inside the template where they are declared.
You cannot use template references to access elements or components in your TypeScript code directly; for that, use @ViewChild.
Template references let you name and access elements or components directly in your template.
They are useful for simple interactions like focusing inputs or reading values.
They keep your code clean by avoiding extra variables or event handlers.
Practice
#templateReference in Angular templates?Solution
Step 1: Understand what template references do
Template references let you name elements or components in the template for direct access.Step 2: Identify their use
They are used to access DOM elements or component instances directly without extra code.Final Answer:
To directly access a DOM element or component instance in the template -> Option DQuick Check:
Template reference = direct element/component access [OK]
- Confusing template references with component declarations
- Thinking they create services
- Assuming they handle data binding
inputBox on an <input> element?Solution
Step 1: Recall Angular template reference syntax
Angular uses the hash symbol (#) to declare template references.Step 2: Check each option
Only#inputBoxis valid syntax for template references on elements.Final Answer:
<input #inputBox> -> Option BQuick Check:
Use # for template references [OK]
- Using ref= instead of #
- Using * which is for structural directives
- Using $ which is invalid here
<input #myInput type="text"> <button (click)="myInput.focus()">Focus</button>
What happens when the button is clicked?
Solution
Step 1: Understand the template reference usage
The#myInputreferences the input element, allowing direct access to its DOM methods.Step 2: Analyze the click event
Click callsmyInput.focus(), which sets keyboard focus on the input element.Final Answer:
The input box receives keyboard focus -> Option CQuick Check:
Template ref calls focus() = input focused [OK]
- Assuming template refs are undefined in event handlers
- Thinking focus() is invalid on input elements
- Expecting button text to change
<input #userInput type="text"> <button (click)="logValue()">Log</button>
And this component code:
logValue() {
console.log(userInput.value);
}What is the problem here?
Solution
Step 1: Understand template reference scope
Template references exist only in the template, not in the component class directly.Step 2: Identify why
Trying to accessuserInputis undefined in the classuserInputin the class without passing it causes an error.Final Answer:
The userInput template reference is not accessible in the component class -> Option AQuick Check:
Template refs not in class scope [OK]
- Assuming template refs are class properties
- Forgetting to pass template ref as method argument
- Confusing template refs with ViewChild
#inputRef, which template code correctly implements this behavior?Solution
Step 1: Understand how to clear input value via template reference
Directly settinginputRef.value = ''clears the input content.Step 2: Check other options for correctness
Options B, C, and D call undefined or non-existent methods:clear()is undefined in the component, andclear()andreset()are not methods on input elements.Final Answer:
<input #inputRef type="text"> <button (click)="inputRef.value = ''">Clear</button> -> Option AQuick Check:
Set inputRef.value to empty string to clear [OK]
- Calling non-existent methods like clear() or reset() on input
- Trying to call component methods without passing template ref
- Confusing template ref usage with reactive forms
Ready to prove it?
Pass the test and this pattern moves unknown
