gpt4 book ai didi

angular - 在 Angular IE11 中捕获粘贴事件

转载 作者:行者123 更新时间:2023-12-04 01:49:13 25 4
gpt4 key购买 nike

我正在寻找一种解决方案,允许我在用户单击 Angular 中的按钮时粘贴剪贴板中存在的内容。

当用户键入 Ctrl+V 时,我使用此代码粘贴数据,但我不知道如何在用户触发事件单击按钮时触发此事件。

constructor() {
document.addEventListener('paste', (e: ClipboardEvent) => {
console.log(e.clipboardData.getData('Text'));
e.preventDefault();
e.stopPropagation();
});
}

copyFromClipBoard(event) {
// My Code goes here
}

最佳答案

您可以使用 Clipboard API访问系统剪贴板。 readText() 方法返回解析为系统剪贴板上复制的文本的 promise :

模板:

<button (click)="handleClick()">Click Me</button>

组件:

handleClick() {
// using bracket notation as TypeScript may complain about `clipboard` property
navigator['clipboard'].readText().then(clipText => {
// do something with copied text here
});
}

这是一个 example在行动中。

希望对您有所帮助!

关于angular - 在 Angular IE11 中捕获粘贴事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54077091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com