Git Product home page Git Product logo

angular-clipboard's Introduction

angular-clipboard

Build Status Test Coverage

Copy text to clipboard by clicking a button, without using Flash. This is using the new Selection API and Clipboard API available in the latest browsers.

Browser support: Chrome 43+, Firefox 41+, Opera 29+, IE10+, Safari 10+ and Mobile Safari 10+.

See the demo.

Install

Install using npm or bower:

$ npm install angular-clipboard --save
$ bower install angular-clipboard --save

angular-clipboard has no other dependencies than Angular itself.

Usage

Require angular-clipboard as a dependency for your app:

angular.module('MyApp', ['angular-clipboard'])
    .controller('MyController', ['$scope', function ($scope) {
        $scope.supported = false;

        $scope.textToCopy = 'I can copy by clicking!';

        $scope.success = function () {
            console.log('Copied!');
        };

        $scope.fail = function (err) {
            console.error('Error!', err);
        };
    }]);

Copy text from an input field by clicking a button:

<input type="text" ng-model="textToCopy">
<button clipboard supported="supported" text="textToCopy" on-copied="success()" on-error="fail(err)">Copy</button>

You can supply a method to be called for the on-copied and on-error event. The on-error function will be called with the error object as argument err.

The optional supported property can be used to detect browser support for the clipboard feature.

Use as service

You can also invoke the copy to clipboard action directly by injecting the clipboard service. Just remember it has to be in a click event, as clipboard access requires user action.

angular.module('MyApp', ['angular-clipboard'])
    .controller('MyController', ['$scope', 'clipboard', function ($scope, clipboard) {
        if (!clipboard.supported) {
            console.log('Sorry, copy to clipboard is not supported');
        }

        $scope.clickHandler = function () {
            clipboard.copyText('Copy this text');
        };
    }]);

Use with a module loader

If you are using a module loader, you can import the module name when requiring it in angular. Works with any AMD/UMD/CommonJS module loader.

import clipboardModule from 'angular-clipboard';

angular.module('mymodule', [clipboardModule.name]);

angular-clipboard's People

Contributors

bubenshchykov avatar coryasilva avatar cyrilgandon avatar ericjperry avatar greenkeeperio-bot avatar lampapetrol avatar mayswind avatar omichelsen avatar rike422 avatar shrpne avatar ziliwesley avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.