Bird
0
0

What is the correct way to access the value of an input element with template reference #userInput inside a button click event?

easy📝 Syntax Q3 of 15
Angular - Component Interaction
What is the correct way to access the value of an input element with template reference #userInput inside a button click event?
A<button (click)="onClick(userInput.value)">Submit</button>
B<button (click)="onClick(#userInput.value)">Submit</button>
C<button (click)="onClick(userInput)">Submit</button>
D<button (click)="onClick(userInput.getValue())">Submit</button>
Step-by-Step Solution
Solution:
  1. Step 1: Understand template reference usage in event binding

    Template references can be used directly in event bindings without the # symbol.
  2. Step 2: Access the input's value property

    To get the input's text, use userInput.value inside the event handler call.
  3. Final Answer:

    <button (click)="onClick(userInput.value)">Submit</button> -> Option A
  4. Quick Check:

    Access input value with userInput.value [OK]
Quick Trick: Use variable name without # inside event bindings [OK]
Common Mistakes:
  • Including # inside event binding expressions
  • Calling non-existent methods like getValue()
  • Passing the whole element instead of its value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes