Home > Blockchain >  Is it possible to declare an empty form group then add form controls afterwards inside the onInit fu
Is it possible to declare an empty form group then add form controls afterwards inside the onInit fu

Time:11-24

As title, how can I declare an empty form group then add form controls afterward? I tried passed in a null or not passing anything at all, its just won't let me do it.

this.demoForm = new FormGroup(null);//DON'T WORK

Any work around? Thankyou.

CodePudding user response:

You can create a new empty FormGroup

const formGroup = new FormGroup({});

and then add a new FormControl/FormGroup/FormArray (to be precise AbstractControl, which is the base class of these three)

const formControl = new FormControl();
formGroup.addControl('controlName', formControl);

For more information you can find the addControl documentation here: https://angular.io/api/forms/FormGroup#addcontrol

  • Related