Bird
0
0

Why does this code cause an error?

medium📝 Debug Q7 of 15
Angular - Templates and Data Binding
Why does this code cause an error?
<input #myInput type="text">
<button (click)="console.log(myInput)">Log</button>
<button (click)="console.log(myInput.value)">Log Value</button>

Assuming myInput is declared only once.
AThe variable must be declared with let or var
BThe template reference variable cannot be used in multiple event bindings
CThe first button logs the element, which is allowed; no error occurs
DAccessing myInput.value causes a runtime error
Step-by-Step Solution
Solution:
  1. Step 1: Understand template ref usage in multiple bindings

    Template refs can be used multiple times in the template without error.
  2. Step 2: Check if logging the element or its value is valid

    Logging the element or its value is valid and does not cause errors.
  3. Final Answer:

    The first button logs the element, which is allowed; no error occurs -> Option C
  4. Quick Check:

    Template refs reusable in template [OK]
Quick Trick: Template refs can be used multiple times safely [OK]
Common Mistakes:
  • Thinking template refs are single-use only
  • Assuming accessing .value causes error
  • Confusing template refs with variables declared in code

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes