How do I copy to clipboard in Angular 2 Typescript?

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

    This is a simple pure Angular2 and javascript solution that doesn't require any libraries and which can be used in an angular component. You could turn it into a service or make it more generic if needed but this will establish the basic idea.

    Currently browsers only allow text to be copied to the clipboard from the Selection in an or

    That's it! The button calls the copyElementText() function in it's component and passes it the ID of the html element to get text from and copy to the clipboard.

    The function uses standard javascript to get the element by it's ID, select it, execute the "Copy" command on the selection and then deselects it.

提交回复
热议问题