I have a dynamic component being created and on ngAfterViewInit I've done the following code:
setTimeout(() => {
this.input.nativeElement.focus();
}, 0);
This works: it sets the focus on the input correctly, but the cursor is placed at the beginning of the value. I would like to place the cursor at the end of the value.
I've tried this:
setTimeout(() => {
this.input.nativeElement.focus();
let start = this.input.nativeElement.selectionStart;
let end = this.input.nativeElement.selectionEnd;
this.input.nativeElement.setSelectionRange(start,end);
}, 0);
I've also tried clearing the value and resetting it but cant get it to work.
Heres the html and how I'm accessing it:
<input class="nav-editor__input" #input type="text">
@ViewChild('input') input: ElementRef;
Any ideas?
FormControlorngModel?FormControl? For me works out of the box without jungling: stackblitz.com/edit/angular-so-53843529<input #input type="text">@ViewChild('input') input: ElementRef;