API for clipboard

API reference for Angular CDK clipboard

import {ClipboardModule} from '@angular/cdk/clipboard';

A service for copying text to the clipboard.

Methods
beginCopy

Prepares a string to be copied later. This is useful for large strings which take too long to successfully render and be copied in the same tick.

The caller must call destroy on the returned PendingCopy.

Parameters

text

string

The string to copy.

Returns
PendingCopy

the pending copy operation.

copy

Copies the provided text into the user's clipboard.

Parameters

text

string

The string to copy.

Returns
boolean

Whether the operation was successful.

Provides behavior for a button that when clicked copies content into user's clipboard.

Selector: [cdkCopyToClipboard]

Properties
Name Description
@Input('cdkCopyToClipboardAttempts')

attempts: number

How many times to attempt to copy the text. This may be necessary for longer text, because the browser needs time to fill an intermediate textarea element and copy the content.

@Input('cdkCopyToClipboard')

text: string

Content to be copied.

@Output('cdkCopyToClipboardCopied')

copied: EventEmitter<boolean>

Emits when some text is copied to the clipboard. The emitted value indicates whether copying was successful.

Methods
copy

Copies the current text to the clipboard.

Parameters

attempts

number = this.attempts

A pending copy-to-clipboard operation.

The implementation of copying text to the clipboard modifies the DOM and forces a re-layout. This re-layout can take too long if the string is large, causing the execCommand('copy') to happen too long after the user clicked. This results in the browser refusing to copy. This object lets the re-layout happen in a separate tick from copying by providing a copy function that can be called later.

Destroy must be called when no longer in use, regardless of whether copy is called.

Methods
copy

Finishes copying the text.

Returns
boolean

destroy

Cleans up DOM changes used to perform the copy operation.

Object that can be used to configure the default options for CdkCopyToClipboard.

Properties
Name Description

attempts: number

Default number of attempts to make when copying text to the clipboard.

Injection token that can be used to provide the default options to CdkCopyToClipboard.

const CDK_COPY_TO_CLIPBOARD_CONFIG: InjectionToken<CdkCopyToClipboardConfig>;
Azure & Blue theme selected.