我创建了一个使用
document.execCommand()方法复制到剪贴板的指令.
原文链接:https://www.f2er.com/angularjs/142866.html指示
(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>