有没有一种方法可以创建一个复制按钮,带有复制功能,可以复制模态框的所有内容,并将其粘贴到记事本中?
由于需要复制的文本是动态的,所以我需要在我的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();
}
附言:
欢迎您现在添加评论,告诉我从控制器操作DOM有多糟糕 manipulate DOM from a Controller。
createElement("pre")
而不是span
,那么您可以复制/粘贴漂亮打印的JSON(这样您的漂亮打印的JSON就不会在分配给span
内的文本节点时变成单行)。 在Chrome中测试过。 - Dimitry K.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>
如果您不想向项目添加新的库并希望自行创建它,这里有一个简单易用的解决方案:
注意:我使用了 Promise 功能来创建它(很棒)
这是 CopyToClipboard.js 模块文件
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>
希望这可以节省您的时间。
document.execCommand
现已弃用。您可以使用以下方法:
HTML:
<i class="fa fa-copy" ng-click="copyToClipboard('some text to copy')"></i>
控制器:
$scope.copyToClipboard = function(string) {
navigator.clipboard.writeText(string)
.then(console.log('copied!'));
}
document.execCommand
现在无法工作。 - Ronald Babung-copyable
指令救了我的一天。 - Mohammedsalim Shivani<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);
}
document.execCommand
现在无法工作。请参考下面@fullstack的答案。 - Ronald Babuapp.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);
}];