0
0
Angularframework~20 mins

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

Choose your learning style9 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.