The code below does not work with pure ngModel binding, so I did a lot of experiments. Latest, also confirmed by Maximillian Schwarzmuller should be the one:
@Directive({
selector: '[ngModel], [formControl]', // or 'input, select, textarea' - but then your controls won't be handled and also checking for undefined would be necessary
})
export class NativeElementInjectorDirective {
constructor(private el: ElementRef, private control : NgControl, @Optional() private model : NgModel) {
if (!! model)
(<any>model.control).nativeElement = el.nativeElement;
else
(<any>control).nativeElement = el.nativeElement;
}
}
So if this directive is provided and exported in the main module, it will attach a custom nativeElement property to all FormControl.
It's a shame it's not coming out-of-the box...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…