A template reference variable is a way to access a DOM element or Angular component directly within the template using a special syntax with the # symbol.
Click to reveal answer
beginner
How do you declare a template reference variable for an input element?
You declare it by adding #variableName inside the input tag, for example: <input #myInput>. This lets you access the input element directly in the template.
Click to reveal answer
intermediate
How can you use a template reference variable to get the value of an input field in Angular?
You can use the variable inside the template, for example: <button (click)="doSomething(myInput.value)">. This passes the current value of the input to the component method.
Click to reveal answer
intermediate
Can template reference variables access Angular components as well as DOM elements?
Yes, if you assign a template reference variable to a component selector, you can access the component instance and call its methods or access its properties directly in the template.
Click to reveal answer
beginner
What is the scope of a template reference variable in Angular templates?
The scope of a template reference variable is limited to the template where it is declared. It cannot be accessed in other templates or component classes directly.
Click to reveal answer
How do you declare a template reference variable named 'inputBox' on an input element?
A<input #inputBox>
B<input ref='inputBox'>
C<input var='inputBox'>
D<input $inputBox>
✗ Incorrect
Template reference variables are declared using the # symbol followed by the variable name inside the element tag.
What can a template reference variable NOT directly access?
AComponent class properties
BAngular components
CDOM elements
DTemplate elements
✗ Incorrect
Template reference variables cannot directly access component class properties; they access elements or component instances in the template.
Which of these is a valid use of a template reference variable in a button click event?
A<button (click)="submit(#inputRef.value)">
B<button (click)="submit(inputRef.value)">
C<button (click)="submit($inputRef.value)">
D<button (click)="submit(ref.inputRef.value)">
✗ Incorrect
You use the variable name directly without # inside event bindings.
If you declare , what does compRef represent?
AThe DOM element of my-comp
BA CSS class applied to my-comp
CA string with the component name
DThe Angular component instance of my-comp
✗ Incorrect
When used on a component selector, the template reference variable points to the component instance.
Where can you use a template reference variable declared in a template?
AInside the component class
BAnywhere in the Angular app
COnly inside the same template
DIn sibling components
✗ Incorrect
Template reference variables are only accessible within the template where they are declared.
Explain how to create and use a template reference variable to get the value of an input field in Angular.
Think about how you can grab the input's current text without using component code.
You got /3 concepts.
Describe the difference between accessing a DOM element and an Angular component using template reference variables.
Consider what you get when you put #var on a normal HTML tag vs a component tag.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a #templateReference in Angular templates?
easy
A. To bind data between components
B. To define a new Angular component
C. To create a service for dependency injection
D. To directly access a DOM element or component instance in the template
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 D
Quick Check:
Template reference = direct element/component access [OK]
Hint: Template references name elements for direct access [OK]
Common Mistakes:
Confusing template references with component declarations
Thinking they create services
Assuming they handle data binding
2. Which of the following is the correct syntax to create a template reference named inputBox on an <input> element?
easy
A.
B.
C.
D.
Solution
Step 1: Recall Angular template reference syntax
Angular uses the hash symbol (#) to declare template references.
Step 2: Check each option
Only #inputBox is valid syntax for template references on elements.
Final Answer:
<input #inputBox> -> Option B
Quick Check:
Use # for template references [OK]
Hint: Use # before name to create template reference [OK]
A. The userInput template reference is not accessible in the component class
B. The logValue method is missing a parameter
C. The console.log statement is incorrect syntax
D. The button click event should be (click)="logValue(userInput)"
Solution
Step 1: Understand template reference scope
Template references exist only in the template, not in the component class directly.
Step 2: Identify why userInput is undefined in the class
Trying to access userInput in the class without passing it causes an error.
Final Answer:
The userInput template reference is not accessible in the component class -> Option A
Quick Check:
Template refs not in class scope [OK]
Hint: Template refs only exist in template, not class [OK]
Common Mistakes:
Assuming template refs are class properties
Forgetting to pass template ref as method argument
Confusing template refs with ViewChild
5. You want to create a form with an input and a button that clears the input when clicked. Using a template reference #inputRef, which template code correctly implements this behavior?
hard
A.
Clear
B.
Clear
C.
Clear
D.
Clear
Solution
Step 1: Understand how to clear input value via template reference
Directly setting inputRef.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, and clear() and reset() are not methods on input elements.
Final Answer:
<input #inputRef type="text">
<button (click)="inputRef.value = ''">Clear</button> -> Option A
Quick Check:
Set inputRef.value to empty string to clear [OK]
Hint: Set inputRef.value = '' to clear input [OK]
Common Mistakes:
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
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.