AngularJS复制到剪贴板 [英] AngularJS copy to clipboard

查看:137
本文介绍了AngularJS复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种使用复制功能制作复制按钮的方法,该功能将复制模式的所有内容,并且可以将其粘贴到记事本中

Is there a way to make a copy button with a copy function that will copy all the contents of a modal and you can paste it to notepad

推荐答案

由于要复制的文本是动态的,因此我在Controller中需要此功能,这是基于 ngClipboard 模块:

I needed this functionality in my Controller, as the text to be copied is dynamic, here's my simple function based on the code in the ngClipboard module:

function share() {
    var text_to_share = "hello world";

    // create temp element
    var copyElement = document.createElement("span");
    copyElement.appendChild(document.createTextNode(text_to_share));
    copyElement.id = 'tempCopyToClipboard';
    angular.element(document.body.append(copyElement));

    // select the text
    var range = document.createRange();
    range.selectNode(copyElement);
    window.getSelection().removeAllRanges();
    window.getSelection().addRange(range);

    // copy & cleanup
    document.execCommand('copy');
    window.getSelection().removeAllRanges();
    copyElement.remove();
}

PS
欢迎您现在添加评论,告诉我从DOM中操纵DOM有多糟.控制器.

P.S.
You're welcome to add a comment now telling me how bad it is to manipulate DOM from a Controller.

这篇关于AngularJS复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆