CngxTypedControl
projects/forms/controls/typed-control.directive.ts
Import#
import { CngxTypedControl } from '@cngx/forms/controls'
Description#
Structural helper that resolves a strongly-typed AbstractControl<T> from the
enclosing FormGroup by name, so reactive-forms templates and host directives
read a control without an as FormControl<T> cast at every access.
Must live inside a [formGroup] - it injects FormGroupDirective non-optionally,
so outside one Angular throws NullInjectorError. As a fail-fast guard it also
throws when no control matches the given name in the parent group.
Read the resolved control through the control signal, via the exportAs ref or
by injecting the directive. In a template ref the generic stays unknown; pass
T explicitly when injecting in TypeScript to get the narrowed control.
<form [formGroup]="form">
<input formControlName="email" cngxTypedControl="email" #email="cngxTypedControl" />
@if (email.control?.hasError('required')) {
<span>Email is required</span>
}
</form>