首页 > 解决方案 > Typescript Angular 7,复制到剪贴板

问题描述

我想制作一个非常简单的输入组,其中包含一个禁用的输入和一个将输入值复制到剪贴板的按钮。当它被复制时,我希望弹出一条小消息说它是。

有什么好的方法可以做到这一点吗?

这种方法似乎不起作用,它看起来像一个快速修复,看起来不太好。

copyInputMessage(inputElement){
    inputElement.select();
    document.execCommand('copy');
    inputElement.setSelectionRange(0, 0);
}

我得到的 html 看起来像这样 atm:

        <div class="input-group">
            <input class="form-control" disabled value="TextToBeCopied" #inputToBeCopied/>
            <button (click)="copyInputMessage(inputToBeCopied)" value="click to copy">COPY</button>
        </div>

我只能找到非常奇怪的解决方案,必须有更简单的方法?

我已经看到了已经存在的解决方案,没有人适合我,也不适合我的问题。

标签: angulartypescriptinputclipboard

解决方案


推荐阅读