This is how I set the focus on a field in Angular 6. First, I declare the field in HTML:
<input type="text" name="myfield" [(ngModel)]="myfield" id="myfield" #focusMe />
Then in Javascript:
@ViewChild('focusMe') focusMe: ElementRef;
.....
this.focusMe.nativeElement.focus();
But I have a table and I need the focus on the first element; I don't know how to set #focusMe
:
<table>
<tr *ngFor="let f of fields; let i = index">
<td>
{{f.name}}
</td>
<td>
<input type="text" name="field{{i}}" [(ngModel)]="f.value"
id="field{{i}}" />
</td>
</tr>
</table>
How to set the focus on the first input field?