Angular Academy · Lesson

FormArray and Dynamic Fields

Add and remove form controls at runtime.

Lesson 2 of 413 steps

FormArray and Dynamic Fields is a free Angular Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, and your progress syncs across the web and the CoddyKit app. The Angular Academy course includes 4 lessons in total.

Dynamic Field Sets

Some forms have a variable number of fields, such as a list of phone numbers or line items. FormArray manages an ordered, growable collection of controls.

Creating a FormArray

A FormArray holds an array of controls or groups.

const phones = new FormArray([
  new FormControl(''),
  new FormControl('')
]);

Inside a FormGroup

Embed the array in a parent group so it is part of the whole form value.

const form = new FormGroup({
  name: new FormControl(''),
  phones: new FormArray<FormControl<string>>([])
});

Adding Controls

Call push to append a new control dynamically.

addPhone() {
  this.phones.push(new FormControl('', { nonNullable: true }));
}

Removing Controls

Use removeAt(index) to delete a specific control.

removePhone(i: number) {
  this.phones.removeAt(i);
}

Rendering with @for

Iterate the array's controls in the template, binding each by index with formControlName.

<div formArrayName="phones">
  @for (ctrl of phones.controls; track $index) {
    <input [formControlName]="$index" />
  }
</div>

The formArrayName Directive

formArrayName links a template block to the array, and the inner formControlName uses the numeric index.

Arrays of Groups

For richer items, store FormGroups in the array, e.g. each contact with a name and number.

const contacts = new FormArray<FormGroup>([]);
contacts.push(new FormGroup({
  label: new FormControl(''),
  number: new FormControl('')
}));

Rendering Group Items

Use formGroupName with the index for each group in the array.

@for (g of contacts.controls; track $index) {
  <div [formGroupName]="$index">
    <input formControlName="label" />
    <input formControlName="number" />
  </div>
}

Reading the Value

The array value is a plain array, in order, reflecting current controls.

const list = this.phones.value; // string[]

Clearing the Array

clear() removes all controls at once, handy when resetting a dynamic section.

this.phones.clear();

Quick Check

Test your understanding of FormArray.

Recap

You learned that FormArray manages a dynamic, ordered collection of controls or groups, using push, removeAt, and clear, rendered with formArrayName plus indexed formControlName/formGroupName.

Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
38
Lessons
144

Frequently Asked Questions

Is the “FormArray and Dynamic Fields” lesson free?

Yes — any 3 lessons of the Angular Academy learning path, including “FormArray and Dynamic Fields”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Angular Academy course includes 4 lessons in total.

What will I learn in “FormArray and Dynamic Fields”?

Add and remove form controls at runtime. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Angular Academy?

No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “FormArray and Dynamic Fields” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Angular Academy lesson?

Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Typed Reactive Forms
  2. FormArray and Dynamic Fields
  3. Async Validators
  4. FormBuilder Patterns
← Back to Angular Academy