Angular 5 - Copy to clipboard

后端 未结 11 1128
旧巷少年郎
旧巷少年郎 2020-11-30 18:26

I am trying to implement an icon that when clicked will save a variable to the user\'s clipboard. I have currently tried several libraries and none of them have been able to

11条回答
  •  没有蜡笔的小新
    2020-11-30 19:03

    I think this is a much more cleaner solution when copying text:

    copyToClipboard(item) {
        document.addEventListener('copy', (e: ClipboardEvent) => {
          e.clipboardData.setData('text/plain', (item));
          e.preventDefault();
          document.removeEventListener('copy', null);
        });
        document.execCommand('copy');
      }
    

    And then just call copyToClipboard on click event in html. (click)="copyToClipboard('texttocopy')"

提交回复
热议问题