I have a front-end angular app in which I would like to have the focus set to a particular textarea with the cursor blinking and ready to have the user type in the text area upon loading.
After doing some Googling I thought that @ViewChild might be the way to go. But so far I have been able to get it to work.
Here is my entire self-contained ts file:
import { Component, ElementRef, ViewChild } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<h1>Hello from {{name}}!</h1>
<textarea #reference placeholder="Start typing"></textarea>
`
})
export class App {
@ViewChild('reference') textarea: ElementRef<HTMLTextAreaElement> | undefined;
name = 'Angular';
ngAfterViewInit(): void {
this.textarea?.nativeElement.focus();
}
}