Last active
March 19, 2018 21:48
-
-
Save sanusart/877cedb8da9fe268582158f0a34e3083 to your computer and use it in GitHub Desktop.
Angular copy to clipboard directive #angular #angular4 #angular5 #directive #clipboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Directive, HostListener, Input } from '@angular/core'; | |
| /** | |
| * @Usage: <a CopyToClipBoard="I am to be copied">Copy to Clipboard</a> | |
| */ | |
| @Directive({ | |
| selector: '[CopyToClipBoard]' | |
| }) | |
| export class CopyToClipBoardDirective { | |
| @Input('CopyToClipBoard') clipboardData: string; | |
| @HostListener('click') onClick() { | |
| function handleCopy(event) { | |
| event.clipboardData.setData('text/plain', this.clipboardData); | |
| event.preventDefault(); | |
| document.removeEventListener('copy', handleCopy, true); | |
| } | |
| document.addEventListener('copy', handleCopy.bind(this), true); | |
| document.execCommand('copy'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment