Angular 5 でテキストをクリップボードにコピーする方法
Angular 5 でテキストをクリップボードにコピーする方法
document.execCommand('copy') を使う
これは最も簡単な方法ですが、ブラウザの互換性が低いという欠点があります。
例
<button (click)="copyText()">コピー</button>
<p #textToCopy>ここにコピーしたいテキスト</p>
import { Component, OnInit, ViewChild } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@ViewChild('textToCopy') textToCopy: ElementRef;
constructor() { }
ngOnInit() {
}
copyText() {
const text = this.textToCopy.nativeElement.textContent;
document.execCommand('copy', false, text);
}
}
Clipboard API
は、ブラウザの互換性が高く、より安全な方法です。
<button (click)="copyText()">コピー</button>
<p #textToCopy>ここにコピーしたいテキスト</p>
import { Component, OnInit, ViewChild } from '@angular/core';
import { Clipboard } from '@angular/cdk/platform-browser';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@ViewChild('textToCopy') textToCopy: ElementRef;
constructor(private clipboard: Clipboard) { }
ngOnInit() {
}
copyText() {
const text = this.textToCopy.nativeElement.textContent;
this.clipboard.copy(text);
}
}
ライブラリを使う
ngx-clipboard
などのライブラリを使うと、より簡単にコピー機能を実装することができます。
<button (click)="copyText()">コピー</button>
<p #textToCopy>ここにコピーしたいテキスト</p>
import { Component, OnInit, ViewChild } from '@angular/core';
import { Clipboard } from 'ngx-clipboard';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@ViewChild('textToCopy') textToCopy: ElementRef;
constructor(private clipboard: Clipboard) { }
ngOnInit() {
}
copyText() {
const text = this.textToCopy.nativeElement.textContent;
this.clipboard.copy(text);
}
}
どの方法を使うかは、プロジェクトの要件や環境に合わせて選ぶと良いでしょう。
document.execCommand('copy') を使う
<button (click)="copyText()">コピー</button>
<p #textToCopy>ここにコピーしたいテキスト</p>
import { Component, OnInit, ViewChild } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@ViewChild('textToCopy') textToCopy: ElementRef;
constructor() { }
ngOnInit() {
}
copyText() {
const text = this.textToCopy.nativeElement.textContent;
document.execCommand('copy', false, text);
}
}
Clipboard API を使う
<button (click)="copyText()">コピー</button>
<p #textToCopy>ここにコピーしたいテキスト</p>
import { Component, OnInit, ViewChild } from '@angular/core';
import { Clipboard } from '@angular/cdk/platform-browser';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@ViewChild('textToCopy') textToCopy: ElementRef;
constructor(private clipboard: Clipboard) { }
ngOnInit() {
}
copyText() {
const text = this.textToCopy.nativeElement.textContent;
this.clipboard.copy(text);
}
}
ライブラリを使う
<button (click)="copyText()">コピー</button>
<p #textToCopy>ここにコピーしたいテキスト</p>
import { Component, OnInit, ViewChild } from '@angular/core';
import { Clipboard } from 'ngx-clipboard';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@ViewChild('textToCopy') textToCopy: ElementRef;
constructor(private clipboard: Clipboard) { }
ngOnInit() {
}
copyText() {
const text = this.textToCopy.nativeElement.textContent;
this.clipboard.copy(text);
}
}
上記は、Angular 5 でテキストをクリップボードにコピーする 3 つの方法のサンプルコードです。
補足
document.execCommand('copy')
を使う方法は、ブラウザの互換性が低いという欠点があります。
Angular 5 でテキストをクリップボードにコピーするその他の方法
ng-content を使う
<button (click)="copyText()">コピー</button>
<div #textToCopy>
ここにコピーしたいテキスト
</div>
import { Component, OnInit, ViewChild } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@ViewChild('textToCopy') textToCopy: ElementRef;
constructor() { }
ngOnInit() {
}
copyText() {
const text = this.textToCopy.nativeElement.textContent;
const el = document.createElement('textarea');
el.value = text;
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
}
}
この方法は、ng-content
を使って、button
要素の外側にあるテキストをコピーします。
@ViewChild と Renderer2 を使う
<button (click)="copyText()">コピー</button>
<p #textToCopy>ここにコピーしたいテキスト</p>
import { Component, OnInit, ViewChild, Renderer2 } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@ViewChild('textToCopy') textToCopy: ElementRef;
constructor(private renderer: Renderer2) { }
ngOnInit() {
}
copyText() {
const text = this.textToCopy.nativeElement.textContent;
const selection = this.renderer.selectRootElement(this.textToCopy.nativeElement);
this.renderer.setSelection(selection);
document.execCommand('copy');
}
}
この方法は、@ViewChild
と Renderer2
を使って、p
要素内のテキストを選択してコピーします。
contenteditable 属性を使う
<p contenteditable="true">ここにコピーしたいテキスト</p>
<button (click)="copyText()">コピー</button>
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
constructor() { }
ngOnInit() {
}
copyText() {
const text = document.getSelection().toString();
document.execCommand('copy');
}
}
この方法は、contenteditable
属性を使って、p
要素を編集可能にして、選択したテキストをコピーします。
- シンプルな方法が良い場合は、
document.execCommand('copy')
を使う方法がおすすめです。 - ブラウザの互換性を重視する場合は、
Clipboard API
を使う方法がおすすめです。 - より柔軟な方法が良い場合は、
ng-content
や@ViewChild
とRenderer2
を使う方法がおすすめです。
angular typescript angular5