Challenge - 5 Problems
Template Reference Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when accessing an input value via template reference?
Consider this Angular template snippet:
What will be logged when the button is clicked?
<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);
}Attempts:
2 left
💡 Hint
Template reference variables allow direct access to DOM elements in the template.
✗ Incorrect
The template reference #userInput points to the input element. Accessing userInput.value gets the current value, which is "Hello" initially.
📝 Syntax
intermediate2: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?Attempts:
2 left
💡 Hint
Template reference variables can be used to call public methods of child components.
✗ Incorrect
Option A correctly assigns a template reference #childComp to the child component and calls its reset() method on button click.
🔧 Debug
advanced2:00remaining
Why does this template reference cause a runtime error?
Template snippet:
Runtime error: "inputRef.focus is not a function". Why?
<app-input #inputRef></app-input>
<button (click)="inputRef.focus()">Focus</button>
Runtime error: "inputRef.focus is not a function". Why?
Attempts:
2 left
💡 Hint
Check what the template reference actually points to.
✗ Incorrect
If the input is wrapped in a component, the template reference points to the component instance, which lacks native DOM methods like focus().
❓ state_output
advanced2:00remaining
What is the value of the template reference variable after user input?
Template:
Component method:
If the user types "Angular" in the input and clicks Show, what is the value of
<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?Attempts:
2 left
💡 Hint
Template reference variables reflect the current state of the DOM element.
✗ Incorrect
The value property of the input element reflects the typed text. Passing nameInput.value sends "Angular" to the method.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about where and how you want to access elements or components.
✗ Incorrect
Template references provide quick access in the template itself, avoiding extra code in the component class for simple tasks.