angular2 clipboard directives
npm install ng2-clipboarda simple component and service for copying text to the user's clipboard.
``bash`
npm i --save ts-clipboard
npm i --save ng2-clipboard
project.config.ts:
`
let additionalPackages: ExtendPackages[] = [{
name: 'ng2-clipboard',
path: 'node_modules/ng2-clipboard/index.js'
},
{
name: 'ng2-font-awesome',
path: 'node_modules/ng2-font-awesome/index.js'
},
{
name: 'ts-clipboard',
path: 'node_modules/ts-clipboard/ts-clipboard.js'
}];
this.addPackagesBundles(additionalPackages);
`
`
import { ClipboardModule } from 'ng2-clipboard';
...
#### ClipboardComponent
all of the below usages are valid:
`html`
}
output
#### ClipboardService
`typescript
import { Component, OnInit } from '@angular/core';
import { ClipboardService } from 'ng2-clipboard/ng2-clipboard';
@Component({
moduleId: module.id,
selector: 'sd-home',
template:
{{someText}},
styleUrls: ['home.component.css'],
})export class HomeComponent implements OnInit {
constructor(private clipboard: ClipboardService) { }
errorMessage: string;
someText: string =
; copyToClipboard = () => { this.clipboard.copy(this.someText); }
ngOnInit() { }
}
`output
$3
` typescript
@Input() content: string; // the text to be copied
@Input() altText: string = 'copy to clipboard'; // the title / altText to be displayed on mouseover
``