Angular 5 - Copy to clipboard

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

    As of Angular Material v9, it now has a clipboard CDK

    Clipboard | Angular Material

    It can be used as simply as

    <button [cdkCopyToClipboard]="This goes to Clipboard">Copy this</button>
    
    0 讨论(0)
  • 2020-11-30 18:51

    Solution 1: Copy any text

    HTML

    <button (click)="copyMessage('This goes to Clipboard')" value="click to copy" >Copy this</button>
    

    .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.value = val;
        document.body.appendChild(selBox);
        selBox.focus();
        selBox.select();
        document.execCommand('copy');
        document.body.removeChild(selBox);
      }
    

    Solution 2: Copy from a TextBox

    HTML

     <input type="text" value="User input Text to copy" #userinput>
          <button (click)="copyInputMessage(userinput)" value="click to copy" >Copy from Textbox</button>
    

    .ts file

        /* To copy Text from Textbox */
      copyInputMessage(inputElement){
        inputElement.select();
        document.execCommand('copy');
        inputElement.setSelectionRange(0, 0);
      }
    

    Demo Here


    Solution 3: Import a 3rd party directive ngx-clipboard

    <button class="btn btn-default" type="button" ngxClipboard [cbContent]="Text to be copied">copy</button>
    

    Solution 4: Custom Directive

    If you prefer using a custom directive, Check Dan Dohotaru's answer which is an elegant solution implemented using ClipboardEvent.


    Solution 5: Angular Material

    Angular material 9 + users can utilize the built-in clipboard feature to copy text. There are a few more customization available such as limiting the number of attempts to copy data.

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2020-11-30 19:01

    You can achieve this using Angular modules:

    navigator.clipboard.writeText('your text').then().catch(e => console.error(e));
    
    0 讨论(0)
  • 2020-11-30 19:02

    Copy using angular cdk,

    Module.ts

    import {ClipboardModule} from '@angular/cdk/clipboard';
    

    Programmatically copy a string: MyComponent.ts,

    class MyComponent {
      constructor(private clipboard: Clipboard) {}
    
      copyHeroName() {
        this.clipboard.copy('Alphonso');
      }
    }
    

    Click an element to copy via HTML:

    <button [cdkCopyToClipboard]="longText" [cdkCopyToClipboardAttempts]="2">Copy text</button>
    

    Reference: https://material.angular.io/cdk/clipboard/overview

    0 讨论(0)
  • 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')"

    0 讨论(0)
提交回复
热议问题