Bird
0
0

How would you correctly initialize a FormArray for multiple phone numbers and add a new empty phone control when a user clicks a button in Angular?

hard📝 component behavior Q8 of 15
Angular - Reactive Forms
How would you correctly initialize a FormArray for multiple phone numbers and add a new empty phone control when a user clicks a button in Angular?
AInitialize with <code>new FormArray([null])</code> and use <code>addControl('phone')</code> on button click
BInitialize with <code>new FormArray([])</code> and use <code>push(new FormControl(''))</code> on button click
CInitialize with <code>new FormArray()</code> and assign a new array on button click
DInitialize with <code>new FormGroup()</code> and use <code>push()</code> on button click
Step-by-Step Solution
Solution:
  1. Step 1: Initialize FormArray empty

    Start with new FormArray([]) to hold phone controls.
  2. Step 2: Add empty FormControl on button click

    Use push(new FormControl('')) to add a blank phone number field.
  3. Step 3: Eliminate incorrect options

    Initialize with new FormArray([null]) and use addControl('phone') on button click uses invalid initialization and method; C reassigns array incorrectly; D uses FormGroup instead of FormArray.
  4. Final Answer:

    Initialize with new FormArray([]) and use push(new FormControl('')) -> Option B
  5. Quick Check:

    Use push() to add empty FormControl [OK]
Quick Trick: Initialize empty FormArray, push new FormControl('') [OK]
Common Mistakes:
MISTAKES
  • Initializing FormArray with null values
  • Using addControl() which belongs to FormGroup
  • Reassigning FormArray instead of pushing controls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes