如何在输入中显示转换值(例如“1(234)567-890”),并且没有转换值('1234567890')?
是否可以为maskInputEl
和制作单独的值maskInput
?
我有模板:
<input #maskInputEl class="spacer" [type]="type"
[formControl]="maskInput"/>
和自定义组件:
export class MaskInputComponent implements ControlValueAccessor, OnInit, OnDestroy {
@ViewChild('maskInputEl') public maskInputEl: ElementRef;
@Input() public mask: any[];
public maskInput = new FormControl();
private _oldValue: string = '';
public ngOnInit(): void {
this.maskInput.valueChanges
.subscribe((value: string) => {
let valid = this.isValidValueByMask(value, this.mask);
if (valid) {
this._oldValue = value;
} else {
value = this._oldValue;
}
this._onChangeCallback(value);
this.onChange.emit(value);
this.maskInputEl.nativeElement.value = value;
},
(err) => console.warn(err)
);
}
public toggleActive(value) {
//
}
public registerOnChange(fn: any): void {
this._onChangeCallback = fn;
}
public registerOnTouched(fn: any): void {
this._onTouchedCallback = fn;
}
public _onChangeCallback: Function = (_: any) => {
//
}
public _onTouchedCallback: Function = (_: any) => {
//
}
public makeActive() {
this.maskInputEl.nativeElement.focus();
}
public writeValue(value: string): void {
this.maskInput.setValue(value);
}
public ngOnDestroy(): void {
//
}
private isValidValueByMask(value: string, mask: RegExp[]): boolean {
//
}
}