Home > database >  Why reset a FormGroup instead of replacing it?
Why reset a FormGroup instead of replacing it?

Time:03-26

I could do myForm.Reset() but I could also do myForm = new FormControl().

I understand the former will avoid subscription leaks if there are subscriptions on the form state, but are there other reasons?

CodePudding user response:

The value of Reset is more apparent when working with a form that contains multiple controls.

this.productForm = this.fb.group({
  productName: ['', [Validators.required,
                     Validators.minLength(3),
                     Validators.maxLength(50)]],
  productCode: ['', Validators.required],
  starRating: ['', NumberValidators.range(1, 5)],
  tags: this.fb.array([]),
  description: ''
});

Then a form reset resets the entire form without having to recreate the entire form group.

  • Related