Skip to main content
cngx-src documentation

adaptFormControl

Functionforms/field

projects/forms/field/form-control-adapter.ts

Description#

Adapts an Angular Reactive Forms AbstractControl (FormControl, FormGroup, FormArray) to the CngxFieldAccessor interface expected by cngx-form-field.

This enables using cngx-form-field without Signal Forms - for teams that haven't migrated yet or for forms that use Reactive Forms by design.

Signature#

adaptFormControl(control: AbstractControl, name: string, destroyRef: DestroyRef)

Parameters#

@paramcontrolAbstractControl

The Reactive Forms control to adapt.

@paramnamestring

A unique field name for deterministic ID generation.

@paramdestroyRefDestroyRef

A DestroyRef for automatic subscription cleanup. Required - without it the three RxJS subscriptions on the control would leak for the lifetime of the parent injector. Pass inject(DestroyRef) from a component field initialiser, or wrap the call in runInInjectionContext.

Returns#

CngxFieldAccessor

A CngxFieldAccessor compatible with [field] input on cngx-form-field.

readonly emailControl = new FormControl('', [Validators.required, Validators.email]);
readonly emailField = adaptFormControl(this.emailControl, 'email', inject(DestroyRef));