how to watch for valueChanges on a control within a formGroup under a dynamic formArray

angular, angular-reactive-forms

Solution

We could do a version of the `valueChanges`, which starts listening when the `variants`-array length is a `>= 3`. Then we listen to each change in the barcode.

I assume you have a function where you add new controls to your `variants` array, we could implement it there:

addVariant() {
  // code here for adding control

  // check length   
  if(this.purchaseInvoiceItemVariantsForm.get('variants').length >= 3) {
    // iterate each object in array
    for(let val of this.purchaseInvoiceItemVariantsForm.get('variants').controls) {
      // listen to changes in each barcode, if change, do something!
      val.get('productBarcode').valueChanges.subscribe(data => {
      console.log(val.get('productBarcode').value)
      // do something!
      })
    }
  } 
}

Demo

Problem

i am new to Angular 2/4 and also to web development in general. I have this form that collects the information of variants of products in a purchase form. I have built a formArray of formGroups of variants as shown in below HTML. ``` <form [formGroup]="this.purchaseInvoiceItemVariantsForm" novalidate> <div formArrayName="variants"> <div *ngFor="let variant of this.purchaseInvoiceItemVariantsForm.controls.variants.controls; let i = index"> <div [formGroupName]="i"> <md-input-container> <input mdInput placeholder="Product Code" formControlName="productBarcode" class="input--small" [readonly]="this.mode == 'view'"> </md-input-container> <md-input-container> <input mdInput placeholder="Color" formControlName="variant1" class="input--small" [readonly]="this.mode == 'view'" required> </md-input-container> <md-input-container> <input mdInput placeholder="Size" formControlName="variant2" class="input--small" [readonly]="this.mode == 'view'" required> </md-input-container> <md-input-container> <input mdInput placeholder="MRP" formControlName="mrp" class="input--small" [readonly]="this.mode == 'view'"> </md-input-container> <md-input-container> <input mdInput placeholder="Purchase Price" formControlName="purchasePrice" class="input--small" [readonly]="this.mode == 'view'" required> </md-input-container> <md-input-container> <input mdInput placeholder="Sell Price" formControlName="sellPrice" class="input--small" [readonly]="this.mode == 'view'" required> </md-input-container> <md-input-container> <input mdInput placeholder="Quantity" formControlName="variantQuantity" class="input--small" [readonly]="this.mode == 'view'" required> </md-input-container> <md-input-container> <input mdInput placeholder="Discount" formControlName="variantDiscount" class="input--small" [readonly]="this.mode == 'view'"> </md-input-container> <button md-icon-button (click)="removeVariant(i)" color="warn" *ngIf="purchaseInvoiceItemVariantsForm.controls.variants.length > 1 && this.mode != 'view'"><md-icon>delete</md-icon></button> </div> </div> ``` Now once the user have added say 3 variants, i want to be able to listen to the valueChanges of the formControl "productBarcode" so that i can fetch the color and size details from the database. Any suggestions how this can be achieved ? thanks in advance! Regards, Navin

Original source