This solution uses the ElementRef
which comes with a warning as its vulnerable to XSS-attacks. And are to be used as a last resort.
HTML:
<ion-item>
<ion-input type="text" #testInput (focus)="onFocus()">
</ion-input>
</ion-item>
TS:
import { Component, ViewChild, ElementRef } from '@angular/core';
...
export class TestPage{
@ViewChild('testInput', { read: ElementRef }) input:ElementRef;
private inputRef;
constructor(){}
ionViewDidLoad(){
this.inputRef = this.input.nativeElement.querySelector('input');
}
onFocus(){
let length = this.inputRef.value.length;
if(length > 3){
this.inputRef.setSelectionRange(length - 3, length)
}
else{
this.inputRef.select();
}
}
...
}