AngularJS复制到剪贴板

use*_*365 18 clipboard angularjs

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

mar*_*mor 18

我需要这个功能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();
}
Run Code Online (Sandbox Code Playgroud)

PS
现在欢迎您添加评论,告诉我从Controller操作DOM有多糟糕.

  • 你是这样的反叛者;) (5认同)

Bas*_*ANI 10

如果您不想向您的项目中添加新库,而是自己创建它,那么这里有一个简单易用的解决方案:

注意:我使用promise功能创建了它(很棒)

这是 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;
                    }
                };
            }];
        }]);
Run Code Online (Sandbox Code Playgroud)

就是这样,多亏了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
            });
        };
   }
Run Code Online (Sandbox Code Playgroud)

最后是HTML

<i class="fa fa-clipboard" data-ng-click="copyHrefToClipboard($event)"></i>
Run Code Online (Sandbox Code Playgroud)

希望这可以节省您的时间


byt*_*0de 9

如果您有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();
                    }
                });
            }
        };
    });
Run Code Online (Sandbox Code Playgroud)

HTML

<a href="" copy-to-clipboard="Text to copy">Copy text</a>
Run Code Online (Sandbox Code Playgroud)


小智 6

document.execCommand现已弃用。相反,你可以这样做:

HTML:

<i class="fa fa-copy" ng-click="copyToClipboard('some text to copy')"></i>
Run Code Online (Sandbox Code Playgroud)

控制器:

$scope.copyToClipboard = function(string) {
    navigator.clipboard.writeText(string)
        .then(console.log('copied!'));          
}
Run Code Online (Sandbox Code Playgroud)


小智 4

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

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

  • 感谢您提供的示例,但它似乎在 Safari 中不起作用。 (2认同)