How do I copy to clipboard in Angular 2 Typescript?

前端 未结 10 1072
-上瘾入骨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:38

    Here is a simple code in case your text is not inside an input or textarea, but a div, or any other HTMLElement and you don't want to use any external libraries:

    window.getSelection().selectAllChildren(document.getElementById('yourID'));
    document.execCommand("copy");
    

    I was unable to use the select() command because it wasn't recognized by Angular. Hope this helps someone!

提交回复
热议问题