Angular中复制功能

原生的复制方法在Angular中并不能使用,找到解决方法如下

import {ElementRef} from '@angular/core'
constructor(private elementRef: ElementRef) {}
  copyDetail() {
    const copyEl = this.elementRef.nativeElement.querySelector('#detail')
    const range = document.createRange()
    range.selectNode(copyEl)
    window.getSelection().removeAllRanges()
    window.getSelection().addRange(range)
    document.execCommand('copy')
    alert('复制成功!')
  }

你可能感兴趣的:(Angular中复制功能)