一个 AngularJS 指令,可在不使用 Flash 的情况下将文本复制到剪贴板
Angularjs directive for clipboard.js by @zenorocha
You can get it on npm.
npm install ngclipboard --saveOr bower, too.
bower install ngclipboard --saveIf you're not into package management, just download a ZIP file.
First, include angularjs and clipboard.js into your document.
Then Include ngclipboard.js.
Add ngclipboard dependency to your module
var myApp = angular.module('app', ['ngclipboard']);Finally, add ngclipboard directive to the wanted html element.
Copy to clipboard
We're living a declarative renaissance, that's why we decided to take advantage of HTML5 data attributes for better usability.
A pretty common use case is to copy content from another element. You can do that by adding a data-clipboard-target attribute in your trigger element.
The value you include on this attribute needs to match another's element selector.
Additionally, you can define a data-clipboard-action attribute to specify if you want to either copy or cut content.
If you omit this attribute, copy will be used by default.
Mussum ipsum cacilds...
Cut to clipboard
As you may expect, the cut action only works on or elements.
Truth is, you don't even need another element to copy its content from. You can just include a data-clipboard-text attribute in your trigger element.
Copy to clipboard
There are cases where you'd like to show some user feedback or capture what has been selected after a copy/cut operation.
That's why we fire custom events such as success and error for you to listen and implement your custom logic.
ngclipboard provides you two attributes called ngclipboard-success and ngclipboard-error to listen the clipboard events and implement your custom logic.
Copy to clipboard
// You can still access the clipboard.js event
$scope.onSuccess = function(e) {
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
e.clearSelection();
};
$scope.onError = function(e) {
console.error('Action:', e.action);
console.error('Trigger:', e.trigger);
}
For a live demonstration, open this site and just your console :)
This library relies on both Selection and execCommand APIs. The first one is supported by all browsers while the second one is supported in the following browsers.
| 42+ ✔ | 12+ ✔ | 41+ ✔ | 9+ ✔ | 29+ ✔ | 10+ ✔ |
The good news is that clipboard.js gracefully degrades if you need to support older browsers. All you have to do is show a tooltip saying Copied! when success event is called and Press Ctrl+C to copy when error event is called because the text is already selected.
MIT License
暂无开放 Issues,或尚未同步最近议题。