Every time I attempt to log a ViewChild
element in either the ngAfterViewInit()
or ngOnInit()
functions, it always logs undefined
in the console.
This is how my HTML file looks:
<input type="number" #phoneNumber>
And here is my TypeScript file:
@ViewChild('phoneNumber',{static:false}) phoneNumber: ElementRef;
ngOnInit(): void {
console.log(this.phoneNumber);
}
ngAfterViewInit():void{
console.log(this.phoneNumber);
}
I have tried all possible ways to access the element using ViewChild
, but nothing seems to work. How can I properly reference my input using the ViewChild
decorator?