How do I copy to clipboard in Angular 2 Typescript?

前端 未结 10 1068
-上瘾入骨i
-上瘾入骨i 2020-12-03 06:45

Is there a way to copy text in clipboard (multi-browser) in Angular2 Typescript framework?

I find only sources of using Javascript, e.g.

document.exe         


        
10条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 07:19

    i got just one method from https://github.com/pehu71/copy-component/blob/master/src/simple/copy.component.ts works even on android 4.1.2

    copy(val) {
    
        let selBox = document.createElement('textarea');
    
        selBox.style.position = 'fixed';
        selBox.style.left = '0';
        selBox.style.top = '0';
        selBox.style.opacity = '0';
        selBox.value = val;
    
        document.body.appendChild(selBox);
        selBox.focus();
        selBox.select();
    
        document.execCommand('copy');
        document.body.removeChild(selBox);
    }
    

提交回复
热议问题