Angular2:控制粘贴的字符串对应于给定的模式

Sci*_*ion 1 angular

如何控制输入中的粘贴字符串对应于给定的模式:

<input type="text" (pasted)="myRegex.match($event)">
Run Code Online (Sandbox Code Playgroud)

因为$event是剪贴板事件,所以它不起作用。

SrA*_*Axi 5

将您的paste活动更改为:

(paste)="onPaste($event)"
Run Code Online (Sandbox Code Playgroud)

在您的组件中,您可以:

onPaste(e: any) {
  let content = myRegex.match(e);  // should return boolean

  if (!content) {
     this.myModel = '';  // We clear it if it doesn't match
  }
}
Run Code Online (Sandbox Code Playgroud)

您的输入应如下所示:

<input type="text" [(ngModel)]="myModel" (paste)="onPaste($event)">
Run Code Online (Sandbox Code Playgroud)