I have a custom form control component in my Angular application, which implements ControlValueAccessor interface.
However, I want to access the F
Using formControlName as an input parameter doesn't work when binding via the [formControl] directive.
Here is a solution that works both ways without any input parameters.
export class MyComponent implements AfterViewInit {
private control: FormControl;
constructor(
private injector: Injector,
) { }
// The form control is only set after initialization
ngAfterViewInit(): void {
const ngControl: NgControl = this.injector.get(NgControl, null);
if (ngControl) {
this.control = ngControl.control as FormControl;
} else {
// Component is missing form control binding
}
}
}