UNPKG

8.6 kBMarkdownView Raw
1[![Financial Contributors on Open Collective](https://opencollective.com/ngx-clipboard/all/badge.svg?label=financial+contributors)](https://opencollective.com/ngx-clipboard) [![travis build](https://img.shields.io/travis/maxisam/ngx-clipboard.svg?style=flat-square)](https://travis-ci.org/maxisam/ngx-clipboard)
2[![npm](https://img.shields.io/npm/dt/ngx-clipboard.svg?style=flat-square)](https://www.npmjs.com/package/ngx-clipboard)
3[![GitHub release](https://img.shields.io/github/release/maxisam/ngx-clipboard.svg?style=flat-square)](https://github.com/maxisam/ngx-clipboard/releases)
4[![npm](https://img.shields.io/npm/l/ngx-clipboard.svg?style=flat-square)]()
5
6# ngx-clipboard , F.K.A [angular2-clipboard](https://www.npmjs.com/package/angular2-clipboard)
7
8From 6.0.0, there is no other JS dependency anymore. Just Angular.
9
10It works with angular version 2.0.0 and up
11
12To make more sense with the future versioning scheme of Angular, the directive selector is now rename to **ngxClipboard**
13
14## Other packages
15
16- [ngx-progressive-image-loader](https://github.com/maxisam/ngx-progressive-image-loader): it can lazy load img/picture, prevent reflow and seo friendly.
17
18## Dependencies
19
20 - If you need to use it on 2.x, please use version 7.x.x.
21 - If you need to use it on 4.x, please use version 8.x.x.
22 - If you need to use it on 5.x, please use version 10.x.x.
23 - If you need to use it on 8.x, please use version 12.x.x.
24 - If you need to use it on 9.x, please use version 13.x.x.
25
26The code are pretty much the same, in v8.0.0 it uses InjectionToken which requires angular4 and above.
27
28## Install
29
30You can get it on npm.
31
32```bat
33npm install ngx-clipboard --save
34```
35
36Open your module file e.g `app.module.ts` and update **imports** array
37
38```ts
39import { ClipboardModule } from 'ngx-clipboard';
40...
41imports: [
42...
43 ClipboardModule,
44...
45]
46```
47
48## Usage
49
50If you use SystemJS to load your files, you might have to update your config:
51
52```js
53System.config({
54 map: {
55 'ngx-clipboard': 'node_modules/ngx-clipboard'
56 }
57});
58```
59
60### Copy source
61
62This library support multiple kinds of copy source.
63
64- Setting `cbContent` attribute
65
66```html
67<button ngxClipboard [cbContent]="'target string'">Copy</button>
68```
69
70You can assign the parent **container** to avoid focus trapper issue, #145
71
72```html
73<div #container>
74 <button ngxClipboard [cbContent]="'target string'" [container]="container">Copy</button>
75</div>
76```
77
78- Setting an input target
79
80```html
81....
82
83<input type="text" #inputTarget />
84
85<button [ngxClipboard]="inputTarget">Copy</button>
86```
87
88- Using `copy` from `ClipboardService` to copy any text you dynamically created.
89
90```ts
91import { ClipboardService } from 'ngx-clipboard'
92
93...
94
95constructor(private _clipboardService: ClipboardService){
96...
97}
98
99copy(text: string){
100 this._clipboardService.copy(text)
101}
102```
103
104### Callbacks
105
106- `cbOnSuccess` callback attribute is triggered after copy was successful with `$event: {isSuccess: true, content: string}`
107
108```html
109<button (cbOnSuccess)="copied($event)" [cbContent]="'example string'">Copied</button>
110```
111
112Or updating parameters directly like so
113
114```html
115<button (cbOnSuccess)="isCopied = true" [cbContent]="'example string'">Copied</button>
116```
117
118- `cbOnError` callback attribute is triggered when there's failure in copying with `$event:{isSuccess: false}`
119
120### Conditionally render host
121
122You can also use the structural directive \*ngxClipboardIfSupported to conditionally render the host element
123
124```html
125<button ngxClipboard *ngxClipboardIfSupported [cbContent]="'target string'" (cbOnSuccess)="isCopied = true">
126 Copy
127</button>
128```
129
130_Special thanks to @surajpoddar16 for implementing this feature_
131
132### Handle copy response globally
133
134To handle copy response globally, you can subscribe to `copyResponse$` exposed by the `ClipboardService`
135
136```
137export class ClipboardResponseService {
138 constructor(
139 private _clipboardService: ClipboardService,
140 private _toasterService: ToasterService
141 ) {
142 this.handleClipboardResponse();
143 }
144
145 handleClipboardResponse() {
146 this._clipboardService.copyResponse$.subscribe((res: IClipboardResponse) => {
147 if (res.isSuccess) {
148 this._toasterService.pop('success', undefined, res.successMessage);
149 }
150 });
151 }
152}
153```
154
155_Special thanks to @surajpoddar16 for implementing this feature_
156
157### Clean up temporary textarea used by this module after each copy to clipboard
158
159This library creates a textarea element at the root of the body for its internal use. By default it only destroys it when the directive is destroyed. If you'd like it to be destroyed after each copy to clipboard, provide root level module configuration like this:
160
161```ts
162ClipboardService.configure({ cleanUpAfterCopy: true });
163```
164
165Special thanks to [@DmitryEfimenko](https://github.com/DmitryEfimenko) for implementing this feature
166
167## Example
168
169[stackblitz.com](https://stackblitz.com/github/maxisam/ngx-clipboard)
170
171## Build project
172
173```cmd
174npm i && npm run build
175```
176
177To run demo code locally
178
179`npm run start`
180
181## Contributing
182
183- Your commits conform to the conventions established [here](https://github.com/conventional-changelog/conventional-changelog-angular/blob/master/convention.md)
184
185## Troubleshooting
186
187Please ask your general questions at https://stackoverflow.com/questions/tagged/ngx-clipboard
188
189## Shoutouts 🙏
190
191Kudos to
192
193[Thierry Templier](http://stackoverflow.com/a/36330518/667767) This project is inspired by his answer on StackOverflow.
194
195The core function is ported from [clipboard.js](http://zenorocha.github.io/clipboard.js/) by [@zenorocha](https://twitter.com/zenorocha).
196
197This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.
198
199<img src="https://www.browserstack.com/images/layout/browserstack-logo-600x315.png" height="80" title="BrowserStack Logo" alt="BrowserStack Logo" />
200
201Big thanks to [BrowserStack](https://www.browserstack.com) for letting the maintainers use their service to debug browser issues.
202
203## Contributors
204
205### Code Contributors
206
207This project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)].
208<a href="https://github.com/maxisam/ngx-clipboard/graphs/contributors"><img src="https://opencollective.com/ngx-clipboard/contributors.svg?width=890&button=false" /></a>
209
210### Financial Contributors
211
212Become a financial contributor and help us sustain our community. [[Contribute](https://opencollective.com/ngx-clipboard/contribute)]
213
214#### Individuals
215
216<a href="https://opencollective.com/ngx-clipboard"><img src="https://opencollective.com/ngx-clipboard/individuals.svg?width=890"></a>
217
218#### Organizations
219
220Support this project with your organization. Your logo will show up here with a link to your website. [[Contribute](https://opencollective.com/ngx-clipboard/contribute)]
221
222<a href="https://opencollective.com/ngx-clipboard/organization/0/website"><img src="https://opencollective.com/ngx-clipboard/organization/0/avatar.svg"></a>
223<a href="https://opencollective.com/ngx-clipboard/organization/1/website"><img src="https://opencollective.com/ngx-clipboard/organization/1/avatar.svg"></a>
224<a href="https://opencollective.com/ngx-clipboard/organization/2/website"><img src="https://opencollective.com/ngx-clipboard/organization/2/avatar.svg"></a>
225<a href="https://opencollective.com/ngx-clipboard/organization/3/website"><img src="https://opencollective.com/ngx-clipboard/organization/3/avatar.svg"></a>
226<a href="https://opencollective.com/ngx-clipboard/organization/4/website"><img src="https://opencollective.com/ngx-clipboard/organization/4/avatar.svg"></a>
227<a href="https://opencollective.com/ngx-clipboard/organization/5/website"><img src="https://opencollective.com/ngx-clipboard/organization/5/avatar.svg"></a>
228<a href="https://opencollective.com/ngx-clipboard/organization/6/website"><img src="https://opencollective.com/ngx-clipboard/organization/6/avatar.svg"></a>
229<a href="https://opencollective.com/ngx-clipboard/organization/7/website"><img src="https://opencollective.com/ngx-clipboard/organization/7/avatar.svg"></a>
230<a href="https://opencollective.com/ngx-clipboard/organization/8/website"><img src="https://opencollective.com/ngx-clipboard/organization/8/avatar.svg"></a>
231<a href="https://opencollective.com/ngx-clipboard/organization/9/website"><img src="https://opencollective.com/ngx-clipboard/organization/9/avatar.svg"></a>