将 html 复制到剪贴板(角度)

use*_*798 5 clipboard angular

有没有办法在 Angular 中将 html 复制到剪贴板?

我正在使用 ngx-clipboard,并尝试格式化复制的文本(即使用粗体、项目符号)

.ts

constructor(private _clipboardService: ClipboardService) {}

callServiceToCopy() {
    this._clipboardService.copyFromContent('<B>This is an important message<\/B>\n These are the details');
}
Run Code Online (Sandbox Code Playgroud)

成分:

<button class="btn btn-primary btn-sm" (click)="callServiceToCopy()">Copy</button>
Run Code Online (Sandbox Code Playgroud)

Stackblitz:https://stackblitz.com/edit/github-ar12tp-irzz84

小智 0

copyToClipboard(id:string): void {
           const from = document.getElementById(id);
           const range = document.createRange();
           window.getSelection().removeAllRanges();
           range.selectNode(from);
           window.getSelection().addRange(range);
           document.execCommand('copy');
           window.getSelection().removeAllRanges();
 }

 <button (click)="copyToClipboard('<B>This is an important message<\/B>\n These are the details')">Copy text</button>
Run Code Online (Sandbox Code Playgroud)