Search code examples
angularangular6angular-reactive-formsangular-formsangular4-forms

Dynamically Add Rows Based on Condition Using Reactive Forms in Angular


Is it possible that i will be able to add rows based on certain condition in Angular reactive forms? If yes, please help me do it. I've already started it in my stackblitz. Please check this link https://stackblitz.com/edit/dynamically-add-rows

this.addForm.get("items").valueChanges.subscribe(val => {
      console.log(val);
      if (val === true) {
        this.addForm.get("items_value").setValue("yes");

        // How to add row here?
        this.addForm.addControl(rows, 'rows')
      }
      if (val === false) {
        this.addForm.get("items_value").setValue("no");

        // How to remove row here?
        this.addForm.removeControl(rows, 'rows')
      }
    });

Solution

  • here is the edited stackblitz :https://stackblitz.com/edit/dynamically-add-rows-gk4veg?file=app%2Fapp.component.html