Tableau UI for Angular
Clipboard
The clipboard service copies text to the clipboard and reads it back.
Import
Inject ClipboardService from tableau-ui-angular/clipboard: one for the app, with no module to import.
Copy and read
| Method | What it does |
|---|---|
writeText(text) | Copies the text |
readText() | Resolves the clipboard's text; the browser may ask the user first |
When the browser refuses, or has no clipboard, as on a page that isn't https or localhost, the call rejects with an error that says why, and the service shows it in an error snack. Block this site's clipboard permission in the browser to see it.
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { TableauUiButtonModule } from 'tableau-ui-angular/button';
import { ClipboardService } from 'tableau-ui-angular/clipboard';
import { SnackService } from 'tableau-ui-angular/snack';
@Component({
selector: 'app-clipboard-basic-example',
imports: [TableauUiButtonModule],
templateUrl: './clipboard-basic.example.html',
styleUrl: './clipboard-basic.example.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ClipboardBasicExampleComponent {
private readonly clipboard = inject(ClipboardService);
private readonly snacks = inject(SnackService);
protected readonly $read = signal<string | undefined>(undefined);
// When the browser refuses, the service shows an error snack and the call rejects, so there is nothing more to do
protected async copy() {
try {
await this.clipboard.writeText('Copied from the Tableau UI docs');
} catch {
return;
}
this.snacks.openSnack('Copied the text', 2000, 'success');
}
protected async read() {
try {
this.$read.set(await this.clipboard.readText());
} catch {
// the service has shown why
}
}
}Handle the error yourself
With false as the last argument, the service shows no snack, and the app says what went wrong.
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { TableauUiButtonModule } from 'tableau-ui-angular/button';
import { ClipboardService } from 'tableau-ui-angular/clipboard';
@Component({
selector: 'app-clipboard-errors-example',
imports: [TableauUiButtonModule],
templateUrl: './clipboard-errors.example.html',
styleUrl: './clipboard-errors.example.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ClipboardErrorsExampleComponent {
private readonly clipboard = inject(ClipboardService);
protected readonly $result = signal('Nothing yet');
// false: the service shows no snack, and the app says what went wrong itself
protected async copy() {
try {
await this.clipboard.writeText('Copied from the Tableau UI docs', false);
this.$result.set('Copied the text');
} catch (error) {
this.$result.set(`Not copied: ${(error as Error).message}`);
}
}
protected async read() {
try {
const text = await this.clipboard.readText(false);
this.$result.set(`The clipboard holds: ${text}`);
} catch (error) {
this.$result.set(`Not read: ${(error as Error).message}`);
}
}
}