如何在angular JS中获取剪贴板数据 [英] how to get clipboard data in angular JS
本文介绍了如何在angular JS中获取剪贴板数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我实际上是想使用 angular JS 来获取剪贴板的内容来模拟复制粘贴的东西.
I was actually looking to get the content of clipboard using angular JS to simulate a copy paste thing.
推荐答案
我创建了一个用于复制到剪贴板的指令,该指令使用 document.execCommand() 方法.
I created a directive for copy to clipboard which is using the document.execCommand() method.
指令
(function() {
app.directive('copyToClipboard', function ($window) {
var body = angular.element($window.document.body);
var textarea = angular.element('<textarea/>');
textarea.css({
position: 'fixed',
opacity: '0'
});
function copy(toCopy) {
textarea.val(toCopy);
body.append(textarea);
textarea[0].select();
try {
var successful = document.execCommand('copy');
if (!successful) throw successful;
} catch (err) {
console.log("failed to copy", toCopy);
}
textarea.remove();
}
return {
restrict: 'A',
link: function (scope, element, attrs) {
element.bind('click', function (e) {
copy(attrs.copyToClipboard);
});
}
}
})
}).call(this);
HTML
<button copy-to-clipboard="Copy Me!!!!" class="button">COPY</button>
这篇关于如何在angular JS中获取剪贴板数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文