[英]Copy to clipboard from textarea in Angular
I am trying to copy text from a HTML textarea, however I only found a solution with an Input tag like this: 我正在尝试从HTML文本区域复制文本,但是我只找到了带有Input标签的解决方案,如下所示:
<input type="text" value="User input Text to copy" #userinput>
<button (click)="copyInputMessage(userinput)" value="click to copy">Copy from Textbox</button>
Function: 功能:
copyInputMessage(inputElement){
inputElement.select();
document.execCommand('copy');
inputElement.setSelectionRange(0, 0);
}
When I want to replace the input tag with a textarea tag it doesn't work anymore. 当我想用textarea标签替换输入标签时,它不再起作用。 Is there an similar easy solution with a textarea?
是否有类似的简单解决方案与textarea?
<textarea cols="30" rows="4" [(ngModel)] = "userinput"></textarea>
<button (click)="copyInputMessage()" value="click to copy">Copy from Textbox</button>
Create a Variable in Ts file As String And Using Property binding bind this with Text area value 在Ts文件中作为字符串创建一个变量,并使用“属性绑定”将此变量与“文本”区域值绑定
userinput: string;
copyInputMessage(this.userinput){
inputElement.select();
document.execCommand('copy');
inputElement.setSelectionRange(0, 0);
}
if You want to use template reference then 如果您想使用模板引用,则
<textarea cols="30" rows="4" #userinput></textarea>
in Ts file 在Ts文件中
@ViewChild('userinput') nameInputRef: ElementRef;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.