15

I was actually looking to get the content of clipboard using angular JS to simulate a copy paste thing.

2
  • Highly recommend angular-zeroclipboard, it is better documented than ng-clip. Also I failed to make ng-clip working. Commented Sep 16, 2014 at 16:08
  • ng-clip depends on ZeroClipboard (at least in some modes of operation), and made it much easier to integrate "Copy to Clipboard" functionality in my case. I followed these few steps to get it working. Commented Nov 15, 2014 at 23:19

5 Answers 5

8

I created a directive for copy to clipboard which is using the document.execCommand() method.

Directive

(function() {
app.directive('copyToClipboard',  function ($window) {
        var body = angular.element($window.document.body);
        var textarea = angular.element('<textarea/>');
        textarea.css({
            position: 'fixed',
            opacity: '0'
        });

        function copy(toCopy) {
            textarea.val(toCopy);
            body.append(textarea);
            textarea[0].select();

            try {
                var successful = document.execCommand('copy');
                if (!successful) throw successful;
            } catch (err) {
                console.log("failed to copy", toCopy);
            }
            textarea.remove();
        }

        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                element.bind('click', function (e) {
                    copy(attrs.copyToClipboard);
                });
            }
        }
    })
}).call(this);    

Html

<button  copy-to-clipboard="Copy Me!!!!" class="button">COPY</button> 
Sign up to request clarification or add additional context in comments.

7 Comments

How might one copy something other than "Copy Me!!!!" - such as , say, the contents of a textarea?
In Controller, use a scope variable $scope.copyMe = ''; and use this var in the textarea value as following. <textarea value="copyMe"> and use the same var in the copy-to-clipboard as following: <button copy-to-clipboard="copyMe" class="button">COPY</button>
When I use this code, textarea.val(toCopy) doesn't append anything into the textarea (my toCopy is text html). When I append it to the body, it actually appends [object Object]. Not sure what's up with that.
@MohammedSafwan Can you please share your code and also are you using Angular 1.5?
@nutboltu I am using AngularJS v1.2.20. Is there any version constraint for using this code snippet?
|
4

here's a concise version I use -

function copyToClipboard(data) {
    angular.element('<textarea/>')
        .css({ 'opacity' : '0', 'position' : 'fixed' })
        .text(data)
        .appendTo(angular.element($window.document.body))
        .select()
        .each(function() { document.execCommand('copy') })
        .remove();
}

2 Comments

Sure looks concise. I wish I could figure out it works. Doesn't copy for me.
angular.element() doesn't include the function appendTo() unless you include the whole jQuery library. Same goes for select()
2

BTW, if using Angular to copy to clipboard with a Chrome Packaged App, do the following:

  1. Add "clipboardRead" and "clipboardWrite" to the "permissions" in the manifest.json.
  2. use ng-click in your view to feed the value to the controller $scope, like: data-ng-click="copyUrlToClipboard(file.webContentLink)"
  3. Put a function in your controller like:

    $scope.copyUrlToClipboard =  function(url) {
        var copyFrom = document.createElement("textarea");
        copyFrom.textContent = url;
        var body = document.getElementsByTagName('body')[0];
        body.appendChild(copyFrom);
        copyFrom.select();
        document.execCommand('copy');
        body.removeChild(copyFrom);
        this.flashMessage('over5');
    }

Comments

0

I had the same issue and I used angular-clipboard feature[1] which uses new Selection API and Clipboard API available in the latest browsers.

First we have to install angular-clipboard lib, i'm using bower.

$ bower install angular-clipboard --save

To import the module use following in html.

<script src="../../bower_components/angular-clipboard/angular-clipboard.js"></script>

To set values to element using $scope in controller

$scope.textToCopy = 'Testing clip board';

Load the clipboard module using,

angular.module('testmodule', ['angular-clipboard']);

This works for Chrome 43+, Firefox 41+, Opera 29+ and IE10+.

Its simple & worked fine.

[1] https://www.npmjs.com/package/angular-clipboard

Thanks,

Comments

0

A completely different approach:

I need to copy & paste text between windows, so I used this to save (copy) the data to local storage. Then, in the other window, I load it out of local storage, using the same key, and I can then 'paste' is as I like.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.