I have a function that recognizes that CTRL-V was pressed and performs the onPaste () function. I intend to do the same, but without using ctrl-v, in this case using a click on the button.
My problem is that if you do ctrl-v everything works, but if you click the button and execute the function, it doesn't work, I get the following error: Cannot read property 'getData' of undefined.
How can I solve this problem?
Thanks
code
@HostListener('paste', ['$event'])
onPaste(e: ClipboardEvent) {
let clipboardData = e.clipboardData || (window as any).clipboardData;
let pastedData = clipboardData.getData('text');
alert(pastedData)
}
<button (click)="onPaste($event)">Do the Same as PASTE (ctrl-v)</button>