Ionic中ion-input的类型为type="number"时,限制输入数量。
废话不多说,直接上代码
html
<ion-input
type="number"
[(ngModel)]="showValue"
(ngModelChange)="change($event)"
></ion-input>
TypeScript关键内容
import { ChangeDetectorRef } from "@angular/core";
constructor(
private cdRef: ChangeDetectorRef
) {}
// 处理数字类型最大可输入数
change(value) {
this.cdRef.detectChanges();
this.showValue = value.length > 8 ? value.substring(0, 8) : value;
}