Alexander Surkov
Alexander Surkov

Reputation: 1721

Angular2 component with clipboardData property

I have an Angular2 component with a method to paste data from the clipboard:

inputPaste(event){
  let clipboardData = event.clipboardData;
  ...

}

This way doesn't work for IE10+, but IE have a window object with a property clipboardData, but typescript compilator throws an error:

inputPaste(event){
  let clipboardData = event.clipboardData 
            || window.clipboardData; //error 'clipboardData' does not exist on type Windows
  ...

}

I have found a solution, that we must use angular2-clipboard directive, but I wan't to use it.

How can I use 'windows.clipboardData' in typescript?

Upvotes: 17

Views: 11051

Answers (2)

piyush nath
piyush nath

Reputation: 89

We can use navigator to get the clipboard data

navigator.clipboard.readText().then(s => console.log(s));

Upvotes: 0

Alexander Surkov
Alexander Surkov

Reputation: 1721

I have found a solution:

inputPaste(event){
    let clipboardData = event.clipboardData 
                        || (<any>window).clipboardData; //typecasting to any
                        or
                        || window['clipboardData']; //access like to array
    ...
}

Upvotes: 27

Related Questions