UNPKG

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