AngularJS复制到剪贴板

时间:2015-03-25 22:16:01

标签: angularjs clipboard

有没有办法制作一个复制按钮,复制功能可以复制模态的所有内容,你可以将它粘贴到记事本

6 个答案:

答案 0 :(得分:11)

我在Controller中需要此功能,因为要复制的文本是动态的,这是基于ngClipboard模块中代码的简单函数:

function share() {
    var text_to_share = "hello world";

    // create temp element
    var copyElement = document.createElement("span");
    copyElement.appendChild(document.createTextNode(text_to_share));
    copyElement.id = 'tempCopyToClipboard';
    angular.element(document.body.append(copyElement));

    // select the text
    var range = document.createRange();
    range.selectNode(copyElement);
    window.getSelection().removeAllRanges();
    window.getSelection().addRange(range);

    // copy & cleanup
    document.execCommand('copy');
    window.getSelection().removeAllRanges();
    copyElement.remove();
}

<强> P.S。
现在欢迎您添加评论,告诉我manipulate DOM from a Controller有多糟糕。

答案 1 :(得分:7)

如果你有jquery支持使用这个指令

.directive('copyToClipboard', function () {
        return {
            restrict: 'A',
            link: function (scope, elem, attrs) {
                elem.click(function () {
                    if (attrs.copyToClipboard) {
                        var $temp_input = $("<input>");
                        $("body").append($temp_input);
                        $temp_input.val(attrs.copyToClipboard).select();
                        document.execCommand("copy");
                        $temp_input.remove();
                    }
                });
            }
        };
    });

HTML

<a href="" copy-to-clipboard="Text to copy">Copy text</a>

答案 2 :(得分:4)

如果您不想为项目添加新库,并且您自己创建它,这是一个简单易用的解决方案:

注意:我创建了具有承诺功能(非常棒)

这里是CopyToClipboard.js module file

angular.module('CopyToClipboard', [])
        .controller('CopyToClipboardController', function () {

        })
        .provider('$copyToClipboard', [function () {

            this.$get = ['$q', '$window', function ($q, $window) {
                var body = angular.element($window.document.body);
                var textarea = angular.element('<textarea/>');
                textarea.css({
                    position: 'fixed',
                    opacity: '0'
                });
                return {
                    copy: function (stringToCopy) {
                        var deferred = $q.defer();
                        deferred.notify("copying the text to clipboard");
                        textarea.val(stringToCopy);
                        body.append(textarea);
                        textarea[0].select();

                        try {
                            var successful = $window.document.execCommand('copy');
                            if (!successful) throw successful;
                            deferred.resolve(successful);
                        } catch (err) {
                            deferred.reject(err);
                            //window.prompt("Copy to clipboard: Ctrl+C, Enter", toCopy);
                        } finally {
                            textarea.remove();
                        }
                        return deferred.promise;
                    }
                };
            }];
        }]);
是的,感谢https://gist.github.com/JustMaier/6ef7788709d675bd8230

现在让我们使用它

angular.module('somthing')
   .controller('somthingController', function ($scope, $copyToClipboard) {
       // you are free to do what you want
            $scope.copyHrefToClipboard = function() {
            $copyToClipboard.copy(/*string to be coppied*/$scope.shareLinkInfo.url).then(function () {
                //show some notification
            });
        };
   }

最后是HTML

<i class="fa fa-clipboard" data-ng-click="copyHrefToClipboard($event)"></i>

希望这可以节省您的时间

答案 3 :(得分:2)

您可以使用我制作的模块ngClipboard。这是链接https://github.com/nico-val/ngClipboard

您可以使用ng-copyable指令或ngClipboard.toClipboard()工厂。

答案 4 :(得分:1)

在HTML中:

<a href="#" ><img src="/Images/copy.png" ng-click="copyToClipboard("TEXT_YOU_WANTTO_COPY")"></img></a>

在控制器中:

$scope.copyToClipboard = function (name) {
    var copyElement = document.createElement("textarea");
    copyElement.style.position = 'fixed';
    copyElement.style.opacity = '0';
    copyElement.textContent =  decodeURI(name);
    var body = document.getElementsByTagName('body')[0];
    body.appendChild(copyElement);
    copyElement.select();
    document.execCommand('copy');
    body.removeChild(copyElement);
}

答案 5 :(得分:0)

试试这个:

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

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

        try {
            var successful = document.execCommand('copy');
            if (!successful)
                throw successful;
           
        } catch (err) {
            window.prompt("Copy to clipboard: Ctrl+C, Enter", toCopy);
        }
        textarea.remove();
    }
}]);

您需要将此服务调用到您的控制器。你可以这样做:

controllers.MyController = ['$scope', 'ngCopy',
function ($scope, ngCopy) {
      ngCopy(copyText);
}];
相关问题