Skip to content

Instantly share code, notes, and snippets.

@sanusart
Last active March 19, 2018 21:48
Show Gist options
  • Select an option

  • Save sanusart/877cedb8da9fe268582158f0a34e3083 to your computer and use it in GitHub Desktop.

Select an option

Save sanusart/877cedb8da9fe268582158f0a34e3083 to your computer and use it in GitHub Desktop.
Angular copy to clipboard directive #angular #angular4 #angular5 #directive #clipboard
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