angular - 使用 Angular2 禁用文本框的剪切、复制和粘贴功能的指令

标签 angular ionic2

我正在使用 Angular2 来限制文本框中的复制和粘贴。但是我该如何编写自定义指令,以便轻松应用所有文本字段。

下面是限制复制和粘贴功能的工作代码。

<ion-input formControlName="confirmpass" type="tel" (cut)="$event.preventDefault()" (copy)="$event.preventDefault()" (paste)="$event.preventDefault()"></ion-input>

最佳答案

您可以使用 HostListener在你的指令中捕捉 cut , pastecopy事件,然后使用 preventDefault()。这是一个例子

import { Directive, HostListener } from '@angular/core';

@Directive({
  selector: '[appBlockCopyPaste]'
})
export class BlockCopyPasteDirective {
  constructor() { }

  @HostListener('paste', ['$event']) blockPaste(e: KeyboardEvent) {
    e.preventDefault();
  }

  @HostListener('copy', ['$event']) blockCopy(e: KeyboardEvent) {
    e.preventDefault();
  }

  @HostListener('cut', ['$event']) blockCut(e: KeyboardEvent) {
    e.preventDefault();
  }
}

像这样使用指令

<ion-input appBlockCopyPaste formControlName="confirmpass" type="tel"></ion-input>

Working demo

关于angular - 使用 Angular2 禁用文本框的剪切、复制和粘贴功能的指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47384952/

相关文章:

api - 向 OPTIONS 请求添加身份验证

javascript - 将 JavaScript\Ionic\Angular 1 应用迁移到 Typescript\Ionic 2\Angular 2 应用

typescript - 如何为 Ionic 2 中的所有请求设置默认 Http header?

angular - 在Ionic2中导入trackingjs作为第三方库

android - cordova 构建 android 错误 : The system cannot find the path specified. 错误:cmd:命令失败,退出代码为 1

asp.net - 如何修复 "Unable to get property ' apply' of undefined or null reference"错误与 asp.net 核心, Angular 4

angular - 如何从父组件中 ngFor 创建的子组件收集输入的数据?

typescript - Angular 2 - 在 (observableData | async) 尚未解析时显示加载信息

angular - Ionic 2 在 navCtrl 按下后保留侧边菜单图标

ionic-framework - ionic 2/ ionic 3-垃圾收集