How to access to an ngModel element inside a reusable component

☆樱花仙子☆ 提交于 2019-12-06 12:03:01

Elements which are tried to be got via ViewChild are not ready at the OnInit life cycle event. You can get the element in AfterViewInit life cycle event.

From the Documentation

View queries are set before the ngAfterViewInit callback is called.

Code block. Access your field in the ngAfterViewInit callback.

@ViewChild('nameAccessor') ngModel:NgModel;

ngOnInit(): void {

}

ngAfterViewInit(): void {
   console.log(this.ngModel);
}

In ngOnInit, the @ViewChild are not yet setted. You should use ngAfterViewInit.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!