Bird
0
0

You want to create a form with an input and a button that clears the input when clicked. Using a template reference #inputRef, which template code correctly implements this behavior?

hard📝 Application Q15 of 15
Angular - Component Interaction
You want to create a form with an input and a button that clears the input when clicked. Using a template reference #inputRef, which template code correctly implements this behavior?
A<input #inputRef type="text"> <button (click)="inputRef.value = ''">Clear</button>
B<input #inputRef type="text"> <button (click)="clear(inputRef)">Clear</button>
C<input #inputRef type="text"> <button (click)="inputRef.clear()">Clear</button>
D<input #inputRef type="text"> <button (click)="inputRef.reset()">Clear</button>
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to clear input value via template reference

    Directly setting inputRef.value = '' clears the input content.
  2. Step 2: Check other options for correctness

    Options B, C, and D call undefined or non-existent methods: clear() is undefined in the component, and clear() and reset() are not methods on input elements.
  3. Final Answer:

    <input #inputRef type="text"> <button (click)="inputRef.value = ''">Clear</button> -> Option A
  4. Quick Check:

    Set inputRef.value to empty string to clear [OK]
Quick Trick: Set inputRef.value = '' to clear input [OK]
Common Mistakes:
  • Calling non-existent methods like clear() or reset() on input
  • Trying to call component methods without passing template ref
  • Confusing template ref usage with reactive forms

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes