0
0
Angularframework~20 mins

Template reference for direct access in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Template Reference Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing an input value via template reference?
Consider this Angular template snippet:
<input #userInput type="text" value="Hello">
<button (click)="logValue(userInput.value)">Log</button>

What will be logged when the button is clicked?
Angular
logValue(value: string) {
  console.log(value);
}
AAn error because #userInput is not accessible in the component
B"Hello"
Cundefined
D"userInput"
Attempts:
2 left
💡 Hint
Template reference variables allow direct access to DOM elements in the template.
📝 Syntax
intermediate
2:00remaining
Which template reference syntax correctly accesses a child component's method?
Given a child component <app-child> with a method reset(), which template code correctly calls reset() from a button click?
A&lt;app-child #childComp&gt;&lt;/app-child&gt;<br>&lt;button (click)="childComp.reset()"&gt;Reset&lt;/button&gt;
B&lt;app-child #childComp&gt;&lt;/app-child&gt;<br>&lt;button (click)="childComp()"&gt;Reset&lt;/button&gt;
C&lt;app-child&gt;&lt;/app-child&gt;<br>&lt;button (click)="#childComp.reset()"&gt;Reset&lt;/button&gt;
D&lt;app-child #childComp&gt;&lt;/app-child&gt;<br>&lt;button (click)="reset()"&gt;Reset&lt;/button&gt;
Attempts:
2 left
💡 Hint
Template reference variables can be used to call public methods of child components.
🔧 Debug
advanced
2:00remaining
Why does this template reference cause a runtime error?
Template snippet:
<app-input #inputRef></app-input>
<button (click)="inputRef.focus()">Focus</button>

Runtime error: "inputRef.focus is not a function". Why?
AThe template reference variable is not accessible inside the (click) event.
BThe input element does not have a focus() method in Angular templates.
CThe template reference points to an Angular component, not a native input element, so focus() is undefined.
DThe input element is missing a closing tag causing the reference to fail.
Attempts:
2 left
💡 Hint
Check what the template reference actually points to.
state_output
advanced
2:00remaining
What is the value of the template reference variable after user input?
Template:
<input #nameInput type="text">
<button (click)="showName(nameInput.value)">Show</button>

Component method:
showName(name: string) {
  this.displayName = name;
}

If the user types "Angular" in the input and clicks Show, what is the value of displayName?
A"" (empty string)
B"nameInput"
Cundefined
D"Angular"
Attempts:
2 left
💡 Hint
Template reference variables reflect the current state of the DOM element.
🧠 Conceptual
expert
2:00remaining
Why use template reference variables instead of @ViewChild for direct access?
Which reason best explains when template reference variables are preferred over @ViewChild in Angular?
ATemplate references allow direct access in the template without needing component class code, useful for simple DOM interactions.
B@ViewChild is deprecated and should not be used anymore.
C@ViewChild only works with native DOM elements, not Angular components.
DTemplate references can access private component properties, while @ViewChild cannot.
Attempts:
2 left
💡 Hint
Think about where and how you want to access elements or components.