File

src/app/shared/directives/copy2clipboard-button.directive.ts

Implements

OnInit

Metadata

Selector [cdCopy2ClipboardButton]

Index

Methods
Inputs
HostListeners

Constructor

constructor(elementRef: ElementRef, renderer: Renderer2, toastr: ToastsManager)
Parameters :
Name Type Optional
elementRef ElementRef No
renderer Renderer2 No
toastr ToastsManager No

Inputs

cdCopy2ClipboardButton
Type : string

HostListeners

click
click()

Methods

Private getInputElement
getInputElement()
Returns : any
ngOnInit
ngOnInit()
Returns : void
import { Directive, ElementRef, HostListener, Input, OnInit, Renderer2 } from '@angular/core';

import { ToastsManager } from 'ng2-toastr';

@Directive({
  selector: '[cdCopy2ClipboardButton]'
})
export class Copy2ClipboardButtonDirective implements OnInit {
  @Input()
  private cdCopy2ClipboardButton: string;

  constructor(
    private elementRef: ElementRef,
    private renderer: Renderer2,
    private toastr: ToastsManager
  ) {}

  ngOnInit() {
    const iElement = this.renderer.createElement('i');
    this.renderer.addClass(iElement, 'icon-prepend');
    this.renderer.addClass(iElement, 'fa');
    this.renderer.addClass(iElement, 'fa-clipboard');
    this.renderer.appendChild(this.elementRef.nativeElement, iElement);
  }

  private getInputElement() {
    return document.getElementById(this.cdCopy2ClipboardButton) as HTMLInputElement;
  }

  @HostListener('click')
  onClick() {
    try {
      // Create the input to hold our text.
      const tmpInputElement = document.createElement('input');
      tmpInputElement.value = this.getInputElement().value;
      document.body.appendChild(tmpInputElement);
      // Copy text to clipboard.
      tmpInputElement.select();
      document.execCommand('copy');
      // Finally remove the element.
      document.body.removeChild(tmpInputElement);

      this.toastr.success('Copied text to the clipboard successfully.');
    } catch (err) {
      this.toastr.error('Failed to copy text to the clipboard.');
    }
  }
}

result-matching ""

    No results matching ""