Angular FormArray Example

The Angular FormArray example shows how to use the FormArray. The FormArray allows us to add controls dynamically to the reactive forms. In this example, we will take a very simple task of dynamically adding/removing skills to an employee form.

Applies to: Angular 2 to the latest edition of i.e. Angular 8. Angular 9, Angular 10

What is FormArray

The FormArray is a way to Manage collection of Form controls in Angular. The controls can be aFormGroup, aFormControl, or another FormArray.

We can group Form Controls in Angular forms in two ways. One is using the FormGroup and the other one is FormArray. The difference is how they implement it. In FormGroup controls becomes a property of the FormGroup. Each control is represented as key-value pair. While in FormArray, the controls become part of an array

Because it is implemented as Array, it makes it easier dynamically add controls.

FormArray Example

Let us build a simple app, which allows us to add the new skill of a person dynamically .

Import FormArray

To use FormArray, First, you need to import the FormArray from the Angular Forms Module.

Build a Form Model

Build a form model skillsForm using the FormBuilder. Our Form has two fields. name of the person and his skills. Since the person can have more than one skill, we define skills as FormArray.

Next, a getter method skills, which returns the skills FormArray from the skillsForm


Best Angular Books
The Top 8 Best Angular Books, which helps you to get started with Angular  

The skill FormGroup

We need to capture two fields under each skill. Name of the skill & years of exp. Hence we create a FormGroup with two fields. The method newSkill creates a new FormGroup and returns it. Note that we won’t be able to assign a name to Form Group.

Dynamically adding skill

Now, we need to add a new skill to the skills FormArray. Since it is an array we can use the push method to add the new skill using the the newSkill method. Note that newSkill() method returns a FormGroup. The name of the FormGroup is its Index in the FormArray.

Dynamically Removing Skill

Use the removeAt method to remove the element from the skills FromArray.

Submit

Template

Now, it is time to build the Template. Use the [formGroup]="skillsForm" to bind the form to the skillsForm model. The formControlName="name" directive binds the name input element to name property of the skillsForm

Binding FormArray to Template

We use the formArrayName directive to bind the skills form array to the div element. Now the div and anything inside the div element is bound to the skills form array.

Inside the div use ngFor to loop through each element of skills FormArray. let i=index will store the index value of the array in template local variable i. We will make use of it to remove the element from the skills array.

Each element under the skills is a FormGroup. We do not have a name to the FormGroup. The Index of the element is automatically assigned as the name for the element.

Hence we use the [formGroupName]="i" where i is the index of the FormArray to bind the FormGroup to the div element.

Finally, we add the controls using the formControlName directive.

Also, pass the index i to removeSkill

Finally, call the addSkills method to add new skills.

That’s it

Angular FormArray Example
Angular FormArray Example App Running

Source Code

app.component.ts

app.component.html

References

  1. FormArray
  2. FromArrayName

Summary

In this tutorial, we learned how to create a simple FormArray Example app.

Complete List of Articles on Angular Forms

  1. Angular Forms Tutorial: Fundamental & Concepts
  2. Template Driven Forms in Angular
  3. Set Value in Template Driven forms in Angular
  4. Reactive Forms in Angular
  5. FormBuilder in Reactive Forms
  6. SetValue & PatchValue in Angular
  7. StatusChanges in Angular Forms
  8. ValueChanges in Angular Forms
  9. FormControl
  10. FormGroup
  11. FormArray Example
  12. Build Dynamic or Nested Forms using FormArray
  13. Validations in Reactive Forms in Angular
  14. Custom Validator in Reactive Forms
  15. Passing Parameter to Custom Validator in Reactive Forms
  16. Inject Service into Custom Validator
  17. Validation in Template Driven Forms
  18. Custom Validator in Template Driven Forms

9 thoughts on “Angular FormArray Example”

    1. *ngFor=”let skill of skills().controls; let i=index”
      needs to be
      *ngFor=”let skill of skills().controls; let i=index”

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top