A FormArray is a way to manage an array of form controls or groups dynamically. It lets you add or remove controls at runtime, useful for forms with variable numbers of inputs.
Click to reveal answer
beginner
How do you add a new control to a FormArray?
You use the push() method on the FormArray instance, passing a new FormControl or FormGroup.
Click to reveal answer
intermediate
Why use FormArray instead of multiple FormControls?
Because FormArray groups controls in a list that can grow or shrink dynamically, making it easier to manage forms with repeating fields like multiple phone numbers or addresses.
Click to reveal answer
beginner
How do you remove a control from a FormArray?
Use the removeAt(index) method, where index is the position of the control you want to remove.
Click to reveal answer
intermediate
What is the role of FormBuilder with FormArray?
FormBuilder helps create FormArray and its controls more easily with less code, improving readability and maintainability.
Click to reveal answer
Which method adds a new control to a FormArray?
Aadd()
Binsert()
Cappend()
Dpush()
✗ Incorrect
The push() method adds a new control to the end of a FormArray.
How do you remove the third control from a FormArray?
Adelete(2)
Bremove(3)
CremoveAt(2)
Dpop()
✗ Incorrect
Controls are zero-indexed, so the third control is at index 2. Use removeAt(2) to remove it.
What type of object can a FormArray hold?
AFormControl or FormGroup
BOnly FormGroup
COnly FormControl
DOnly primitive values
✗ Incorrect
FormArray can hold both FormControl and FormGroup instances.
Which Angular service helps create FormArray easily?
ARenderer2
BFormBuilder
CRouter
DHttpClient
✗ Incorrect
FormBuilder is the service designed to simplify creating form controls and arrays.
What is a common use case for FormArray?
ADynamic lists of inputs like multiple emails
BStatic single input forms
CRouting configuration
DStyling components
✗ Incorrect
FormArray is ideal for dynamic lists of inputs such as multiple email addresses or phone numbers.
Explain how to create a dynamic form with multiple input fields using FormArray in Angular.
Think about how you would add or remove items like a list of phone numbers.
You got /4 concepts.
Describe the benefits of using FormArray over individual FormControls for dynamic forms.
Consider a form where the number of inputs changes based on user actions.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of FormArray in Angular forms?
easy
A. To manage a dynamic list of form controls that can grow or shrink
B. To create a single form control with a fixed value
C. To style form inputs dynamically
D. To handle form submission events
Solution
Step 1: Understand what FormArray does
FormArray is designed to hold multiple form controls dynamically, allowing adding or removing controls as needed.
Step 2: Compare options
Only To manage a dynamic list of form controls that can grow or shrink correctly describes this dynamic list management. Others describe unrelated features.
Final Answer:
To manage a dynamic list of form controls that can grow or shrink -> Option A
Quick Check:
FormArray = dynamic list of controls [OK]
Hint: FormArray is for lists of controls, not single controls [OK]
Common Mistakes:
Confusing FormArray with FormControl
Thinking FormArray handles styling
Assuming FormArray manages form submission
2. Which of the following is the correct way to add a new control to a FormArray named emails?
easy
A. emails.insertControl(new FormControl(''))
B. emails.addControl(new FormControl(''))
C. emails.push(new FormControl(''))
D. emails.appendControl(new FormControl(''))
Solution
Step 1: Recall FormArray methods
The method to add a control to FormArray is push().
Step 2: Check each option
Only emails.push(new FormControl('')) uses push() correctly. Others are invalid methods for FormArray.
Final Answer:
emails.push(new FormControl('')) -> Option C
Quick Check:
Use push() to add control [OK]
Hint: Use push() to add controls to FormArray [OK]
Common Mistakes:
Using addControl() which is for FormGroup
Trying appendControl() or insertControl() which don't exist
Confusing FormArray with FormGroup methods
3. Given this code snippet, what will be the length of the FormArray after execution?
const tags = new FormArray([]);
tags.push(new FormControl('angular'));
tags.push(new FormControl('typescript'));
tags.removeAt(0);
medium
A. 1
B. 2
C. 0
D. 3
Solution
Step 1: Track push operations
Two controls are added, so length becomes 2.
Step 2: Apply removeAt(0)
The first control is removed, reducing length by 1.
Final Answer:
1 -> Option A
Quick Check:
2 added - 1 removed = 1 [OK]
Hint: Count pushes minus removes for length [OK]
Common Mistakes:
Forgetting removeAt reduces length
Assuming removeAt(0) removes last item
Counting pushes only without removals
4. Identify the error in this code snippet that tries to remove a control from a FormArray named phones:
phones.remove(1);
medium
A. remove() requires a FormControl argument, not an index
B. No error; remove() works fine
C. remove() should be called with a string index
D. remove() is not a method of FormArray; should use removeAt()
Solution
Step 1: Check FormArray removal methods
FormArray uses removeAt(index) to remove controls by position.
Step 2: Analyze the code
Calling remove(1) is invalid because remove is not a FormArray method.
Final Answer:
remove() is not a method of FormArray; should use removeAt() -> Option D
Quick Check:
Use removeAt() to remove by index [OK]
Hint: Use removeAt() to remove by index, not remove() [OK]
Common Mistakes:
Using remove() instead of removeAt()
Passing wrong argument types to removal methods
Assuming remove() exists on FormArray
5. You want to create a form where users can add multiple email addresses dynamically. Which approach correctly initializes the FormArray and adds a new empty email control when a button is clicked?
hard
A. Initialize with new FormGroup([]) and add with emails.push('')
B. Initialize with new FormArray([]) and add with emails.push(new FormControl(''))
C. Initialize with new FormControl([]) and add with emails.addControl(new FormControl(''))
D. Initialize with new FormArray(['']) and add with emails.insertControl(new FormControl(''))
Solution
Step 1: Correct initialization of FormArray
FormArray should be initialized with an empty array: new FormArray([]).
Step 2: Adding new controls properly
Use push(new FormControl('')) to add a new empty control dynamically.
Final Answer:
Initialize with new FormArray([]) and add with emails.push(new FormControl('')) -> Option B