So I have an input field which I would like to focus on load in an Angular application. I read that the Renderer2 way is the good method of doing so, and it works if I call the function for example on click like so:
focusMyInput() {
this.renderer.selectRootElement('#myInput').focus();
}
But if I try to make it focus on load, like this:
ngAfterViewInit() {
this.renderer.selectRootElement('#myInput').focus();
}
It throws an error:
The selector "#myInput" did not match any elements
at DefaultDomRenderer2.selectRootElement
HTML:
<input id="myInput" />
Any idea what could be the reason behind this?