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(''));
}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('')); } }
Think about what happens when you push a new control into an empty FormArray.
Initially, the FormArray is empty with length 0. Calling addEmail() pushes one new FormControl, increasing the length to 1.
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);
}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); } }
Removing the control at index 1 removes the second element.
Removing the control at index 1 removes the control holding 'b'. The remaining values are 'a' and 'c'.
Which of the following code snippets correctly creates a FormArray of FormControls from an array of strings ['x', 'y', 'z'] using FormBuilder?
Remember to call this.fb.control as a function with the value.
Option C correctly maps each string to a FormControl initialized with that string. Option C uses new FormControl(value) which is valid but requires importing FormControl. Option C passes the function reference without calling it. Option C misses passing the value to the constructor.
Given this code, why does calling addEmail() cause a runtime error?
emails = this.fb.array();
addEmail() {
this.emails.push('test@example.com');
}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'); } }
Check the type of argument that push expects.
The push method of FormArray expects a FormControl or AbstractControl instance. Passing a string directly causes a runtime error.
When rendering a FormArray in Angular templates with *ngFor, which strategy helps Angular efficiently update the DOM when controls are added or removed?
Think about how Angular identifies list items uniquely in *ngFor.
Using a trackBy function helps Angular identify which controls changed, added, or removed, improving rendering performance. Angular does not track by value automatically, and OnPush affects change detection but not tracking. Recreating the entire FormArray is inefficient.