Bird
Raised Fist0
Angularframework~20 mins

FormArray for dynamic fields in Angular - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FormArray Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when adding a new control to a FormArray?

Given this Angular component snippet, what will be the length of the emails FormArray after calling addEmail() once?

this.emails = this.fb.array([]);

addEmail() {
  this.emails.push(this.fb.control(''));
}
Angular
import { Component } from '@angular/core';
import { FormBuilder, FormArray } from '@angular/forms';

@Component({
  selector: 'app-email-list',
  template: ''
})
export class EmailListComponent {
  emails: FormArray;

  constructor(private fb: FormBuilder) {
    this.emails = this.fb.array([]);
  }

  addEmail() {
    this.emails.push(this.fb.control(''));
  }
}
AThrows an error
Bundefined
C0
D1
Attempts:
2 left
💡 Hint

Think about what happens when you push a new control into an empty FormArray.

❓ state_output
intermediate
2:00remaining
What is the value of the FormArray after removing a control?

Consider this FormArray initialized with three controls holding values 'a', 'b', and 'c'. After calling removeEmail(1), what is the value of emails.value?

this.emails = this.fb.array([
  this.fb.control('a'),
  this.fb.control('b'),
  this.fb.control('c')
]);

removeEmail(index: number) {
  this.emails.removeAt(index);
}
Angular
import { Component } from '@angular/core';
import { FormBuilder, FormArray } from '@angular/forms';

@Component({
  selector: 'app-email-list',
  template: ''
})
export class EmailListComponent {
  emails: FormArray;

  constructor(private fb: FormBuilder) {
    this.emails = this.fb.array([
      this.fb.control('a'),
      this.fb.control('b'),
      this.fb.control('c')
    ]);
  }

  removeEmail(index: number) {
    this.emails.removeAt(index);
  }
}
A['a', 'c']
B['a', 'b', 'c']
C['b', 'c']
DThrows an error
Attempts:
2 left
💡 Hint

Removing the control at index 1 removes the second element.

📝 Syntax
advanced
2:00remaining
Which option correctly initializes a FormArray with dynamic controls?

Which of the following code snippets correctly creates a FormArray of FormControls from an array of strings ['x', 'y', 'z'] using FormBuilder?

Athis.fb.array(['x', 'y', 'z'].map(value => new FormControl(value)))
Bthis.fb.array(['x', 'y', 'z'].map(value => this.fb.control))
Cthis.fb.array(['x', 'y', 'z'].map(value => this.fb.control(value)))
Dthis.fb.array(['x', 'y', 'z'].map(value => new FormControl()))
Attempts:
2 left
💡 Hint

Remember to call this.fb.control as a function with the value.

🔧 Debug
advanced
2:00remaining
Why does this FormArray push cause a runtime error?

Given this code, why does calling addEmail() cause a runtime error?

emails = this.fb.array();

addEmail() {
  this.emails.push('test@example.com');
}
Angular
import { Component } from '@angular/core';
import { FormBuilder, FormArray } from '@angular/forms';

@Component({
  selector: 'app-email-list',
  template: ''
})
export class EmailListComponent {
  emails: FormArray;

  constructor(private fb: FormBuilder) {
    this.emails = this.fb.array([]);
  }

  addEmail() {
    this.emails.push('test@example.com');
  }
}
ABecause push expects a FormControl, not a string
BBecause emails is not initialized
CBecause push is not a method of FormArray
DBecause FormArray cannot be empty
Attempts:
2 left
💡 Hint

Check the type of argument that push expects.

🧠 Conceptual
expert
3:00remaining
How does Angular track FormArray controls for efficient rendering?

When rendering a FormArray in Angular templates with *ngFor, which strategy helps Angular efficiently update the DOM when controls are added or removed?

AManually recreating the entire FormArray on each change
BUsing a <code>trackBy</code> function that returns the control's unique id or index
CUsing <code>ChangeDetectionStrategy.OnPush</code> disables updates for FormArray
DAngular automatically tracks controls by their value without extra code
Attempts:
2 left
💡 Hint

Think about how Angular identifies list items uniquely in *ngFor.

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

  1. Step 1: Understand what FormArray does

    FormArray is designed to hold multiple form controls dynamically, allowing adding or removing controls as needed.
  2. 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.
  3. Final Answer:

    To manage a dynamic list of form controls that can grow or shrink -> Option A
  4. 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

  1. Step 1: Recall FormArray methods

    The method to add a control to FormArray is push().
  2. Step 2: Check each option

    Only emails.push(new FormControl('')) uses push() correctly. Others are invalid methods for FormArray.
  3. Final Answer:

    emails.push(new FormControl('')) -> Option C
  4. 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

  1. Step 1: Track push operations

    Two controls are added, so length becomes 2.
  2. Step 2: Apply removeAt(0)

    The first control is removed, reducing length by 1.
  3. Final Answer:

    1 -> Option A
  4. 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

  1. Step 1: Check FormArray removal methods

    FormArray uses removeAt(index) to remove controls by position.
  2. Step 2: Analyze the code

    Calling remove(1) is invalid because remove is not a FormArray method.
  3. Final Answer:

    remove() is not a method of FormArray; should use removeAt() -> Option D
  4. 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

  1. Step 1: Correct initialization of FormArray

    FormArray should be initialized with an empty array: new FormArray([]).
  2. Step 2: Adding new controls properly

    Use push(new FormControl('')) to add a new empty control dynamically.
  3. Final Answer:

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

    FormArray + push(new FormControl('')) = dynamic emails [OK]
Hint: Use FormArray([]) and push(new FormControl('')) to add fields [OK]
Common Mistakes:
  • Using FormControl instead of FormArray for multiple inputs
  • Trying to add controls with addControl() on FormArray
  • Initializing FormArray with values instead of controls
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard