When loading a component, I want you to focus on the first element after initialization. I found a tutorial for this, but it doesn't want to work for me.
https://davidmcintosh.medium.com/auto-focusing-an-angular-input-the-easy-way-part-1-dcb1799e025f
But if I put it in a timer that is 0 then it works great. But I don't want to use timer.
ngAfterViewInit(): void {
timer(0).subscribe(() => this.firstElement.nativeElement.focus());
}
Why do I need a timer with such a ridiculous value?
Here is my HTML code:
<p class="news-title" tabindex="0" #firstElement>{{ 'news.title' | translate }}</p>
<ul class="news-list" role="list">
<li role="listitem" tabindex="0" class="list-item">{{ 'news.item-1' | translate }}</li>
<li role="listitem" tabindex="0" class="list-item">{{ 'news.item-2' | translate }}</li>
<li role="listitem" tabindex="0" class="list-item">{{ 'news.item-3' | translate }}</li>
</ul>