I'm using ngModelController
for formatting using input
element. But I'm unable to set the caret position, if the user tries to enter any digit in the beginning or in the middle. As the caret alway jumps at the end.
What is the best place for calling set cursor?
ngModelController.$parsers.unshift(function (viewValue) {
var plainNumber = viewValue.replace(/ /g, '');
// $log.info(`viewValue = ${viewValue}`);
console.log(`$modelValue = ${ngModelController.$modelValue} | $viewValue = ${ngModelController.$viewValue}`);
console.log(`cursorPos = ${cursor.getCursorPos(elem[0])}`);
var newVal = ""
for (var i = 0; i < plainNumber.length; i++) {
if (i === 3 || i === 6) {
newVal += " ";
}
newVal += plainNumber[i];
}
cursor.setCursorPos(cursor.getCursorPos(elem[0]) + 1, elem[0]);
// $log.info(`newVal = ${newVal}`);
elem.val(newVal);
return plainNumber;
});