angular - ionic 格式输入元素 onblur

标签 angular ionic-framework ionic2

我正在尝试使用以下指令格式化输入元素 onblur

@Directive({
  selector: '[formatnumbercomma]' // Attribute selector
})
export class FormatNumberCommaDirective {

  private e1: HTMLInputElement;
  numberPipe = new DecimalPipe('en-US');

  constructor(private elementRef: ElementRef) {
    this.e1 = this.elementRef.nativeElement;
  }

  @HostListener("blur", ["$event.target.value"])
  onBlur(value) {
    this.e1.value = this.numberPipe.transform(value, '.0-0');
  }
}

html如下:

<ion-input class="input-panel-2" 
          type="number" 
          name="a"
          required
          formatnumbercomma
          [(ngModel)]="amount" 
          (ngModelChange)="computeX($event)"
          #pVar="ngModel">
</ion-input>

问题是输入值没有更新为格式化值。

感谢您的帮助,

阿什利

最佳答案

尝试使用 ionic 的 native 事件进行输入,一旦输入失去焦点,该事件就会触发。

<ion-input type="text" formControlName="name" (focusout)="onBlur()"></ion-input>

在 Angular 中,

onBlur(){
   alert("input lost focus");
}

关于angular - ionic 格式输入元素 onblur,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46784576/

相关文章:

ionic-framework - 通过更新 google-services 插件的版本来解决版本冲突 | ionic

angular - Ionic 2 异常处理不起作用?

ios - ionic 1 - Cordova - 谷歌地图 : Disappeared buttons on map - only IOS

css - Ionic TextAreas 变得太大

css - 如何根据屏幕尺寸使用 CSS 隐藏或显示内容?

javascript - 为 *ngFor 内的项目赋值

angular - 使用 VMWare Clarity Design 和 Angular2+ 隐藏可扩展数据网格上的插入符号列

angular - 带有@angular/flex-layout、@angular/material 和带有图像的mat-card 的响应式网格?

Angular 9 "No value provided for @angular/core symbol ' ɵɵinjectPipeChangeDetectorRef'。”(IVY)

javascript - 无法读取未定义的属性 '[function]'