Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何添加遮罩';yyyy/yyyy';在angular2中,使用管道和格式化程序输入字段_Regex_Angular_Angular2 Forms_Angular2 Directives_Angular2 Pipe - Fatal编程技术网

Regex 如何添加遮罩';yyyy/yyyy';在angular2中,使用管道和格式化程序输入字段

Regex 如何添加遮罩';yyyy/yyyy';在angular2中,使用管道和格式化程序输入字段,regex,angular,angular2-forms,angular2-directives,angular2-pipe,Regex,Angular,Angular2 Forms,Angular2 Directives,Angular2 Pipe,我想知道如何使用正则表达式为输入字段创建掩码 我需要向字段添加一些掩码,例如:yyyy/yyy。这听起来像个句号 我看到一个链接,它使用Angular2中的管道创建遮罩。这是你的电话号码 因此,我想用不同的正则表达式创建一个管道,以允许用户只写以下内容:yyyy/yyyy;并利用管道的变换方法。 这可能吗 这是我的管道和格式化程序: import { Pipe, PipeTransform } from "@angular/core"; @Pipe({ name: "mypipe" }) exp

我想知道如何使用正则表达式为输入字段创建掩码

我需要向字段添加一些掩码,例如:yyyy/yyy。这听起来像个句号

我看到一个链接,它使用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);
  }
}