Useful Development

Angular reactive forms: FormArrays with OnPush change detection

posted on Jun 27, 2019

Large reactive forms can be painful to work with unless you break them down into smaller components. Using OnPush change detection with those components can improve performance. Doing these two things together can lead to some unexpected behaviour around FormArrays which, judging by the comments on Stack Overflow at least leads many developers to give up and switch the change detection back to default. I've created a StackBlitz sample to show a way to get round these issues (see below).


The sample has a simple form consisting of a single FormArray called things. The user can disable/enable the form, add a new thing, load up the form array with existing values and remove an existing value. Child components have been created for the FormArray (ThingsComponent) and the 'thing' FormGroup (ThingComponent). Both of the components have the change detection strategy set to OnPush. Here are the key points regarding how to get everything working:

Make the components responsible for creating the FormGroup

This isn't essential but keeping the definition for the FormGroup in one place, namely the FormGroup's component avoids errors that can occur typing out the definition in multiple places. This is done by exporting a function that creates the FormGroup and exporting it from the component.

export function ThingFormGroup(formBuilder: FormBuilder){

The component containing the FormArray should listen for value changes

The ThingsComponent is responsible for display ingthe Thing components. This is the first potential stumbling block for OnPush. As the property itself is not updated on a change to the FormArray the component needs to listen for values changes to the FormArray and manually call change detection. To do this in a way where the component itself handles everything without the consumer having to worry about it the ThingsComponent uses a getter and setter on the formArray to subscribe/unsubscribe form the formArray valueChanges.

  selector: 'app-things',
  templateUrl: './things.component.html',
  styleUrls: ['./things.component.css'],
  changeDetection: ChangeDetectionStrategy.OnPush
export class ThingsComponent implements OnDestroy {
  private _formArray: FormArray;
  set formArray(formArray: FormArray) {
    this._formArray = formArray;

    if (this.subscriptions.formArrayChanges) {

    this.subscriptions.formArrayChanges = this.formArray.valueChanges.subscribe(
      result => {
  get formArray() {
    return this._formArray;

  private subscriptions: { [key: string]: Subscription } = {};

    private changeDetectorRef: ChangeDetectorRef,
    private formBuilder: FormBuilder
  ) { }

  ngOnDestroy() {
    Object.keys(this.subscriptions).forEach(sk =>

  add() {

  remove(formGroup: FormGroup) {

Another benefit of having the formArray as a property is that is avoids getting an error when trying to implicitly convert from AbstractControl when compiling with AOT.

Loading values into the FormArray

This is another pain point with FormArrays, in order to add values to the form it is necessary to add the FormGroups to the FormArray first, then patch the value.

 load() {
    const things = [{
      id: 1,
      name: 'one'
    }, {
      id: 2,
      name: 'two'
    }, {
      id: 3,
      name: 'three'

    things.forEach(thing => {
      (this.formGroup.get('things') as FormArray).push(


Removing an item from the FormArray

The ThingComponent has a button which raises an event called remove with the formGroup as the event argument when it is clicked.

export class ThingComponent {
  @Output() remove = new EventEmitter();
  @Input() formGroup: FormGroup;

  constructor() {}

<mat-card [formGroup]="formGroup" fxLayout="row wrap" fxLayoutAlign="start center" fxLayoutGap="12px">
    <mat-form-field fxFlex="1 1 auto">
        <input matInput placeholder="id" formControlName="id" />
  <mat-form-field fxFlex="1 1 auto">
    <input matInput placeholder="name" formControlName="name" />
    fxFlex="0 0 auto"

The ThingsComponent listens for this event and removes the formGroup from the form array. The ThingComponent has a button which raises an event called remove with the formGroup as the event argument when it is clicked.

	<app-thing fxFlex="1 1 auto" *ngFor="let thingFormGroup of formArray.controls; let i = index" (remove)="remove($event)"

remove(formGroup: FormGroup) {

Making the formArray component reusable

If this is approach is looking like a bit of a pain to do multiple times throughout a big project there is some good news. With only a couple of changes the ThingsComponent can be made into a generic component that can be used for all FormArrays. For the final code check out this fork of the original sample. The ThingsCompnent has been renamed to FormArrayComponent. The add function was specific to the ThingComponent so that has been changed to just being an output event so the parent component can handle adding any type of FormGroup required.

export class FormArrayComponent implements OnDestroy {
  @Output() add = new EventEmitter();

Also instead of having the ThingComponent declared in the template ang-content tag is ued so that the child component for the FormGroup can be transcluded into it.

 <mat-card class="form-array" fxLayout="column" fxLayoutGap="12px">

Lastly form has been changed to use the generic form array component.

   <form [formGroup]="formGroup">
            fxFlex="1 1 auto"
              let thingFormGroup of things.formArray.controls;
              let i = index