How to get FormControl instance from ControlValueAccessor
One possible solution is to get NgControl
instance via Injector
:
import { NgControl } from '@angular/forms';
export class PasswordComponent implements ControlValueAccessor, AfterViewInit {
...
ngControl: NgControl;
constructor(private inj: Injector) {
...
}
ngAfterViewInit() {
this.ngControl = this.inj.get(NgControl)
}
then you can get status like
ngControl.control.status
See also
- Access valid value of custom form control
It looks like injector.get(NgControl) is throwing a deprecation warning, so I wanted to chime in with another solution:
constructor(public ngControl: NgControl) {
ngControl.valueAccessor = this;
}
The trick is to also remove NG_VALUE_ACCESSOR from the providers array otherwise you get a circular dependency.
More information about this is in this talk by Kara Erickson of the Angular team.