Dynamic Forms: FormArray, Nested Groups and Custom Controls

Angular 16 min min read Updated: Nov 16, 2026 Advanced
Dynamic Forms: FormArray, Nested Groups and Custom Controls
Advanced Topic 4 of 5

FormArray: Repeating Rows

typescript @Component({ selector: 'app-invoice-form', imports: [ReactiveFormsModule, CurrencyPipe], templateUrl: './invoice-form.html', }) export class InvoiceForm { private fb = inject(FormBuilder); form = this.fb.nonNullable.group({ customer: ['', Validators.required], lines: this.fb.array([this.newLine()]), }); get lines() { return this.form.controls.lines; } newLine() { return this.fb.nonNullable.group({ description: ['', Validators.required], qty: [1, [Validators.required, Validators.min(1)]], price: [0, [Validators.required, Validators.min(0)]], }); } addLine() { this.lines.push(this.newLine()); } removeLine(i: number) { this.lines.removeAt(i); } total = toSignal( this.form.valueChanges.pipe( startWith(null), map(() => this.lines.getRawValue().reduce((s, l) => s + l.qty * l.price, 0))), { initialValue: 0 }); save() { if (this.form.invalid) { this.form.markAllAsTouched(); return; } console.log(this.form.getRawValue()); } }
html <form [formGroup]="form" (ngSubmit)="save()"> <input formControlName="customer" placeholder="Customer"> <div formArrayName="lines"> @for (line of lines.controls; track line; let i = $index) { <div [formGroupName]="i" class="row"> <input formControlName="description" placeholder="Item"> <input formControlName="qty" type="number"> <input formControlName="price" type="number"> <button type="button" (click)="removeLine(i)" [disabled]="lines.length === 1">Remove</button> </div> } </div> <button type="button" (click)="addLine()">Add line</button> <p>Total: {{ total() | currency }}</p> <button [disabled]="form.invalid">Save invoice</button> </form>

Build a Form from Configuration

typescript interface FieldConfig { key: string; label: string; type: 'text' | 'email' | 'number' | 'select'; required?: boolean; options?: string[]; } export class DynamicForm { fields = input.required<FieldConfig[]>(); private fb = inject(FormBuilder); form = computed(() => { const controls: Record<string, FormControl> = {}; for (const f of this.fields()) { controls[f.key] = new FormControl('', { nonNullable: true, validators: f.required ? Validators.required : [] }); } return new FormGroup(controls); }); }
html <form [formGroup]="form()"> @for (f of fields(); track f.key) { <label>{{ f.label }} @switch (f.type) { @case ('select') { <select [formControlName]="f.key"> @for (o of f.options; track o) { <option [value]="o">{{ o }}</option> } </select> } @default { <input [type]="f.type" [formControlName]="f.key"> } } </label> } </form>

Custom Form Control (ControlValueAccessor)

To use your own component with formControlName or ngModel (for example a star rating) implement ControlValueAccessor.

typescript @Component({ selector: 'app-star-rating', template: ` @for (s of [1,2,3,4,5]; track s) { <button type="button" [disabled]="disabled()" (click)="select(s)" [class.on]="s <= value()">&#9733;</button> } `, providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => StarRating), multi: true }], }) export class StarRating implements ControlValueAccessor { value = signal(0); disabled = signal(false); private onChange: (v: number) => void = () => {}; private onTouched: () => void = () => {}; writeValue(v: number | null) { this.value.set(v ?? 0); } registerOnChange(fn: (v: number) => void) { this.onChange = fn; } registerOnTouched(fn: () => void) { this.onTouched = fn; } setDisabledState(isDisabled: boolean) { this.disabled.set(isDisabled); } select(v: number) { this.value.set(v); this.onChange(v); this.onTouched(); } }
html <app-star-rating formControlName="rating" />
Library alternative: Libraries such as Angular Material, PrimeNG and Formly already provide many form controls. Build your own only when you need something special.

Conclusion

FormArray handles repeating groups of fields, forms can be built from configuration, and ControlValueAccessor turns any component into a real form control.

Quick Summary: FormArray + formArrayName + [formGroupName]=i; ControlValueAccessor for custom inputs.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators