FormArray dùng khi số lượng control động, ví dụ nhiều số điện thoại, danh sách địa chỉ, line items trong invoice hoặc survey questions.
Ví dụ thêm phone control động:
typescript
const phones = new FormArray([
new FormControl("", { nonNullable: true }),
])
phones.push(new FormControl("", { nonNullable: true }))Nó giữ collection controls theo index và cho phép push/remove/insert.
- Nếu mỗi item có nhiều field, thường dùng
FormArray<FormGroup<...>>. - Cần cẩn thận validation và
tracktrong template để không làm mất state khi thêm/xóa dòng.
FormArray is used when the number of controls is dynamic, such as multiple phone numbers, addresses, invoice line items or survey questions.
Example dynamic phone controls:
typescript
const phones = new FormArray([
new FormControl("", { nonNullable: true }),
])
phones.push(new FormControl("", { nonNullable: true }))It stores a collection of controls by index and allows push/remove/insert.
- If each item has many fields, use
FormArray<FormGroup<...>>. - Be careful with validation and template
trackso row state is not lost when adding/removing items.