百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

ngclipboard

> 编程语言
开源

一个 AngularJS 指令,可在不使用 Flash 的情况下将文本复制到剪贴板

385 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

一个 AngularJS 指令,可在不使用 Flash 的情况下将文本复制到剪贴板

ngclipboard

An angularjs directive to copy text to clipboard without using flash

Angularjs directive for clipboard.js by @zenorocha

Install

You can get it on npm.

bash
npm install ngclipboard --save

Or bower, too.

bower install ngclipboard --save

If you're not into package management, just download a ZIP file.

Setup

First, include angularjs and clipboard.js into your document.

xml

Then Include ngclipboard.js.

xml

Add ngclipboard dependency to your module

javascript
var myApp = angular.module('app', ['ngclipboard']);

Finally, add ngclipboard directive to the wanted html element.

javascript

    Copy to clipboard

Usage

We're living a declarative renaissance, that's why we decided to take advantage of HTML5 data attributes for better usability.

Copy text from another element

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.

xml

    

Cut text from another element

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.

xml

Mussum ipsum cacilds...

    Cut to clipboard

As you may expect, the cut action only works on or elements.

Copy text from attribute

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.

xml

    Copy to clipboard

Events

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.

xml

    Copy to clipboard
javascript
// 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 :)

Browser Support

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.

License

MIT License

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言