When to use FormGroup vs. FormArray?

前端 未结 4 1716
庸人自扰
庸人自扰 2020-11-27 11:50

FormGroup:

A FormGroup aggregates the values of each child FormControl into one object, with each control name as the key.

4条回答
  •  北海茫月
    2020-11-27 12:28

    From: Anton Moiseev Book “Angular Development with Typescript, Second Edition.” :

    When you need to programmatically add (or remove) controls to a form, use FormArray. It’s similar to FormGroup but has a length variable. Whereas FormGroup represents an entire form or a fixed subset of a form’s fields, FormArray usually represents a collection of form controls that can grow or shrink.

    For example, you could use FormArray to allow users to enter an arbitrary number of emails.

提交回复
热议问题