0
0
Angularframework~20 mins

Template reference variables in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Template Reference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Angular template using a template reference variable?

Consider this Angular component template:

<input #userInput type="text" value="Hello">
<button (click)="onClick(userInput.value)">Show</button>
<p>{{message}}</p>

And this component class:

message = '';
onClick(value: string) {
  this.message = value;
}

What will be displayed in the paragraph after clicking the button?

Angular
<input #userInput type="text" value="Hello">
<button (click)="onClick(userInput.value)">Show</button>
<p>{{message}}</p>
AThe paragraph shows 'Hello'
BThe paragraph shows an empty string
CThe paragraph shows 'userInput.value'
DThe paragraph shows 'undefined'
Attempts:
2 left
💡 Hint

Think about what #userInput refers to and what userInput.value accesses.

📝 Syntax
intermediate
1:30remaining
Which option correctly declares a template reference variable for a element?

Choose the correct syntax to declare a template reference variable named myInput on an <input> element.

A&lt;input var-myInput type="text"&gt;
B&lt;input let-myInput type="text"&gt;
C&lt;input #myInput type="text"&gt;
D&lt;input ref-myInput type="text"&gt;
Attempts:
2 left
💡 Hint

Template reference variables use a specific prefix symbol.

🔧 Debug
advanced
2:30remaining
Why does this Angular template cause an error?

Given this template snippet:

<input #inputRef type="text">
<button (click)="logValue()">Log</button>

And this component code:

logValue() {
  console.log(inputRef.value);
}

Why does this cause a runtime error?

Angular
<input #inputRef type="text">
<button (click)="logValue()">Log</button>
AinputRef is not defined in the component class, so it causes a ReferenceError
BinputRef.value is undefined because the input has no value attribute
CThe template reference variable cannot be used inside methods without passing it as a parameter
DThe button click event is not bound correctly
Attempts:
2 left
💡 Hint

Consider where inputRef is accessible in Angular.

state_output
advanced
2:00remaining
What is the value of 'count' after clicking the button twice?

Consider this Angular component template:

<button #btn (click)="increment(btn.textContent)">Click me</button>
<p>Count: {{count}}</p>

And this component class:

count = 0;
increment(text: string) {
  if (text === 'Click me') {
    this.count += 1;
  }
}

What is the value of count after clicking the button twice?

Angular
<button #btn (click)="increment(btn.textContent)">Click me</button>
<p>Count: {{count}}</p>
A0
B1
CNaN
D2
Attempts:
2 left
💡 Hint

Check what btn.textContent returns and how increment updates count.

🧠 Conceptual
expert
2:30remaining
Which statement about Angular template reference variables is TRUE?

Choose the correct statement about template reference variables in Angular.

AThey can be accessed directly inside the component class without passing them as parameters.
BThey can reference any DOM element or Angular component/directive instance within the template scope.
CThey are only usable inside <code>&lt;ng-template&gt;</code> elements.
DThey automatically update the component class properties without event bindings.
Attempts:
2 left
💡 Hint

Think about what template reference variables can point to and their scope.