入力フィールドの正規表現を使用してマスクを作成する方法を知りたいです。
yyyy/yyyy のように、いくつかのマスクをフィールドに追加する必要があります。これはピリオドのように聞こえます。
Angular2 のパイプを使用してマスクを作成するリンクを見ました。ここにリンクがあります。
したがって、別の正規表現でパイプを作成して、ユーザーがこれだけを記述できるようにしたいと思います: yyyy/yyyy ; パイプから変換メソッドを使用します。これは可能ですか?
ここに私のパイプとフォーマッタがあります:
import { Pipe, PipeTransform } from "@angular/core";
@Pipe({ name: "mypipe" })
export class MyPipe implements PipeTransform {
private SEPARATOR: string;
constructor() {
this.SEPARATOR = "/";
}
transform(value): string {
let integer = (value || "").toString();
// Here is where the code should be, to transform the value
return integer;
}
transform(value): string {
// parse method
}
}
import { Directive, HostListener, ElementRef, OnInit } from "@angular/core";
// import { MyPipe } from ...
@Directive({ selector: "[myFormatter]" })
export class MyFormatterDirective implements OnInit {
private el: HTMLInputElement;
constructor(
private elementRef: ElementRef,
private mypipe: MyPipe
) {
this.el = this.elementRef.nativeElement;
}
ngOnInit() {
this.el.value = this.mypipe.transform(this.el.value);
}
@HostListener("keydown", ["$event.target.value"])
handleKeyboardEvent(value) {
this.el.value = this.mypipe.transform(value);
}
}