0

I have an input in with a custom pipe that adds a space every 4 characters inserted. The issue is that if I insert for example "1234 5678" and I go and delete 4, the focus instead of staying there to insert a new character it moves to the end.

PIPE

import { Pipe, PipeTransform } from "@angular/core";

@Pipe({
  name: 'formatBankAcc'
})

export class FormatBankAccPipe implements PipeTransform {
  transform(value: string) {
    if (value != null) {
      value = value.replace(/[^\dA-Z]/g, '')
      .replace(/(.{4})/g, '$1 ')
      .trim();
    }
    return value;
  }
}

HTML

<ion-input #editInput type="{{ !servicesPerBillingAccountViewModel.editable ? 'hidden' : 'text' }}" maxlength="29" [ngModel]="billingAccount.bankAccountNumber | formatBankAcc" 
    (ngModelChange)="billingAccount.bankAccountNumber=$event" 
    [class.input-border-bottom-grey]="billingAccount.showConfirmCancelIcons"
    [class.input-border-validation-error]="billingAccount.showLabelError"
    [placeholder]="servicesPerBillingAccountsCmsModel.literalBankPlaceholder" 
    (keyup)="checkBankAccLength(billingAccount)">
</ion-input>
Victor York
  • 1,621
  • 4
  • 20
  • 55
  • You can try to look into these solutions. See if any of these helps: [Input cursor position jumps to end with ng-change()](https://stackoverflow.com/q/20203216), [Angular2 keyup event update ngModel cursor position jumps to end](https://stackoverflow.com/q/40931845). – palaѕн May 14 '20 at 09:24
  • also, you can try to add `ngModelOptions` to `ion-input` like `[ngModelOptions]="{updateOn: 'blur'}"` – palaѕн May 14 '20 at 09:28
  • Thats not working for me :( – Victor York May 14 '20 at 11:42

0 Answers0