Bird
0
0

Consider this template code:

medium📝 Debug Q14 of 15
Angular - Templates and Data Binding
Consider this template code:
<input #userInput type="text">
<button (click)="logValue()">Log</button>

And component code:
logValue() {
  console.log(userInput.value);
}

What is the problem here?
AThe template reference variable should be declared with $ sign
BuserInput is not defined in the component class, causing an error
ClogValue() should be an arrow function
DThere is no problem; it works fine
Step-by-Step Solution
Solution:
  1. Step 1: Identify scope of template reference variables

    Template reference variables exist only in the template, not in the component class.
  2. Step 2: Check component method usage

    The method tries to access userInput directly, which is undefined in the class, causing an error.
  3. Final Answer:

    userInput is not defined in the component class, causing an error -> Option B
  4. Quick Check:

    Template refs not accessible in class directly [OK]
Quick Trick: Template refs only work inside template, not class methods [OK]
Common Mistakes:
  • Trying to use template refs inside component class directly
  • Thinking template refs need $ or other symbols
  • Assuming arrow functions fix scope issues

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes