I have a custom form control component in my Angular application, which implements ControlValueAccessor
interface.
However, I want to access the F
Here is a simplified/cleaned up version of the accepted answer that works for both FormControlName and FormControl inputs:
export class CustomFormComponent implements ControlValueAccessor, OnInit {
@Input() formControl: FormControl;
@Input() formControlName: string;
// get ahold of FormControl instance no matter formControl or formControlName is given.
// If formControlName is given, then controlContainer.control is the parent FormGroup/FormArray instance.
get control() {
return this.formControl || this.controlContainer.control.get(this.formControlName);
}
constructor(private controlContainer: ControlContainer) { }
}