Is it possible to get native element for formControl?

前端 未结 5 508
面向向阳花
面向向阳花 2020-12-03 09:36

I\'ve got Angular2 reactive form. I created formControls and assigned it to input fields by[formControl]=.... As I understand it creates nati

5条回答
  •  半阙折子戏
    2020-12-03 10:11

    Added minor fix to baHI answer(moved logic to OnInit). Error mentioned in comments is probably connected to changes in forms. This answer is for "@angular/forms": "~7.1.0",

        @Directive({
          selector: '[ngModel]'
        })
        export class NativeElementInjectorDirective implements OnInit {
            constructor (private el: ElementRef, private control : NgControl) {}
    
            ngOnInit(){
              (this.control.control as any).nativeElement = this.el.nativeElement;
            }
        }
    

提交回复
热议问题