Angular 5 - Copy to clipboard

后端 未结 11 1114
旧巷少年郎
旧巷少年郎 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:05

    First suggested solution works, we just need to change

    selBox.value = val;
    

    To

    selBox.innerText = val;
    

    i.e.,

    HTML:

    
    

    .ts file:

    copyMessage(val: string){
        const selBox = document.createElement('textarea');
        selBox.style.position = 'fixed';
        selBox.style.left = '0';
        selBox.style.top = '0';
        selBox.style.opacity = '0';
        selBox.innerText = val;
        document.body.appendChild(selBox);
        selBox.focus();
        selBox.select();
        document.execCommand('copy');
        document.body.removeChild(selBox);
      }
    

提交回复
热议问题