Answers:
解决方案1:复制任何文本
的HTML
<button (click)="copyMessage('This goes to Clipboard')" value="click to copy" >Copy this</button>
.ts文件
copyMessage(val: string){
const selBox = document.createElement('textarea');
selBox.style.position = 'fixed';
selBox.style.left = '0';
selBox.style.top = '0';
selBox.style.opacity = '0';
selBox.value = val;
document.body.appendChild(selBox);
selBox.focus();
selBox.select();
document.execCommand('copy');
document.body.removeChild(selBox);
}
解决方案2:从文本框复制
的HTML
<input type="text" value="User input Text to copy" #userinput>
<button (click)="copyInputMessage(userinput)" value="click to copy" >Copy from Textbox</button>
.ts文件
/* To copy Text from Textbox */
copyInputMessage(inputElement){
inputElement.select();
document.execCommand('copy');
inputElement.setSelectionRange(0, 0);
}
解决方案3:导入第三方指令ngx-clipboard
<button class="btn btn-default" type="button" ngxClipboard [cbContent]="Text to be copied">copy</button>
解决方案4:自定义指令
如果您更喜欢使用自定义指令,请查看Dan Dohotaru的答案,这是一个使用实施的优雅解决方案ClipboardEvent
。
Cannot read property 'select' of undefined
角度6。这个角度6兼容吗?
<input *ngIf="invitation_code" type="text" readonly value="{{invitation_code}}" #userinput > <button *ngIf="code_success" (click)="copyInputMessage(userinput)" value="click to copy" > Copy code </button>
谢谢
position
,left
,top
,和opacity
。并将其替换为selBox.style.height = '0';
我知道这已经在这里获得了很高的投票,但是我宁愿使用自定义指令方法,并像@jockeisorby所建议的那样依赖ClipboardEvent,同时还要确保正确删除了侦听器(需要提供相同的功能)对于添加和删除事件侦听器)
stackblitz 演示
import { Directive, Input, Output, EventEmitter, HostListener } from "@angular/core";
@Directive({ selector: '[copy-clipboard]' })
export class CopyClipboardDirective {
@Input("copy-clipboard")
public payload: string;
@Output("copied")
public copied: EventEmitter<string> = new EventEmitter<string>();
@HostListener("click", ["$event"])
public onClick(event: MouseEvent): void {
event.preventDefault();
if (!this.payload)
return;
let listener = (e: ClipboardEvent) => {
let clipboard = e.clipboardData || window["clipboardData"];
clipboard.setData("text", this.payload.toString());
e.preventDefault();
this.copied.emit(this.payload);
};
document.addEventListener("copy", listener, false)
document.execCommand("copy");
document.removeEventListener("copy", listener, false);
}
}
然后这样使用
<a role="button" [copy-clipboard]="'some stuff'" (copied)="notify($event)">
<i class="fa fa-clipboard"></i>
Copy
</a>
public notify(payload: string) {
// Might want to notify the user that something has been pushed to the clipboard
console.info(`'${payload}' has been copied to clipboard`);
}
注意:请注意,window["clipboardData"]
因为IE无法理解,e.clipboardData
我认为这是复制文本时更加干净的解决方案:
copyToClipboard(item) {
document.addEventListener('copy', (e: ClipboardEvent) => {
e.clipboardData.setData('text/plain', (item));
e.preventDefault();
document.removeEventListener('copy', null);
});
document.execCommand('copy');
}
然后只需在html中的click事件上调用copyToClipboard即可。(click)=“ copyToClipboard('texttocopy')”
从Angular Material v9开始,它现在具有剪贴板CDK
它可以像
<button [cdkCopyToClipboard]="This goes to Clipboard">Copy this</button>
jockeisorby答案的修改版本,修复了未正确删除事件处理程序的问题。
copyToClipboard(item): void {
let listener = (e: ClipboardEvent) => {
e.clipboardData.setData('text/plain', (item));
e.preventDefault();
};
document.addEventListener('copy', listener);
document.execCommand('copy');
document.removeEventListener('copy', listener);
}
document.execCommand(‘cut’/‘copy’) was denied because it was not called from inside a short running user-generated event handler
您可以使用Angular模块实现此目的:
navigator.clipboard.writeText('your text').then().catch(e => console.error(e));
以下方法可用于复制邮件:
export function copyTextAreaToClipBoard(message: string) {
const cleanText = message.replace(/<\/?[^>]+(>|$)/g, '');
const x = document.createElement('TEXTAREA') as HTMLTextAreaElement;
x.value = cleanText;
document.body.appendChild(x);
x.select();
document.execCommand('copy');
document.body.removeChild(x);
}
在Angular中做到这一点并使代码保持简单的最佳方法是使用此项目。
https://www.npmjs.com/package/ngx-clipboard
<fa-icon icon="copy" ngbTooltip="Copy to Clipboard" aria-hidden="true"
ngxClipboard [cbContent]="target value here"
(cbOnSuccess)="copied($event)"></fa-icon>
使用角度cdk复制
模块
import {ClipboardModule} from '@angular/cdk/clipboard';
以编程方式复制字符串:MyComponent.ts,
class MyComponent {
constructor(private clipboard: Clipboard) {}
copyHeroName() {
this.clipboard.copy('Alphonso');
}
}
单击一个元素以通过HTML复制:
<button [cdkCopyToClipboard]="longText" [cdkCopyToClipboardAttempts]="2">Copy text</button>
首先建议的解决方案有效,我们只需要更改
selBox.value = val;
至
selBox.innerText = val;
即
HTML:
<button (click)="copyMessage('This goes to Clipboard')" value="click to copy" >Copy this</button>
.ts文件:
copyMessage(val: string){
const selBox = document.createElement('textarea');
selBox.style.position = 'fixed';
selBox.style.left = '0';
selBox.style.top = '0';
selBox.style.opacity = '0';
selBox.innerText = val;
document.body.appendChild(selBox);
selBox.focus();
selBox.select();
document.execCommand('copy');
document.body.removeChild(selBox);
}