Saturday, January 5, 2019

When to use FormGroup vs. FormArray?


FormGroup:
A FormGroup aggregates the values of each child FormControl into one object, with each control name as the key.
const form = new FormGroup({
  first: new FormControl('Nancy', Validators.minLength(2)),
  last: new FormControl('Drew'),
});
FormArray:
A FormArray aggregates the values of each child FormControl into an array.
const arr = new FormArray([
  new FormControl('Nancy', Validators.minLength(2)),
  new FormControl('Drew'),
]);

Answer :
FormArray is a variant of FormGroup. The key difference is that its data gets serialized as an array (as opposed to being serialized as an object in case of FormGroup). This might be especially useful when you don’t know how many controls will be present within the group, like dynamic forms.

No comments:

Post a Comment

48 answers on StackOverflow to the most popular Angular questions I gathered the most common questions and answers from Stac...