Angular 5 - Copy to clipboard

后端 未结 11 1105
旧巷少年郎
旧巷少年郎 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 18:51

    Below method can be used for copying the message:-

    export function copyTextAreaToClipBoard(message: string) {
      const cleanText = message.replace(/<\/?[^>]+(>|$)/g, '');
      const x = document.createElement('TEXTAREA') as HTMLTextAreaElement;
      x.value = cleanText;
      document.body.appendChild(x);
      x.select();
      document.execCommand('copy');
      document.body.removeChild(x);
    }
    

提交回复
热议问题