Copy button in textarea in Ionic

Asked

Viewed 287 times

0

I’m trying to create a button that makes a CTRL+C in textarea, with javascript, it is possible to make a select() in the document.getElementById, and after the one document.execCommand('copy'), but how I’m working with Ionic, that way it doesn’t work, how could it be done ?

2 answers

0

Use the plugin Clipboard, see below:

Installation

ionic cordova plugin add cordova-clipboard
npm install @ionic-native/clipboard

Insert into modules

Add the Clipboard in the modules of AppModule, see below:

import { Clipboard } from '@ionic-native/clipboard';

....

@NgModule({
  imports: [
  ...
  providers: [Clipboard]
  ...
})

export class AppModule {}

Insert on page

Finally insert on the page, below the function onCopy will put the text passed on it to clipboard:

import { Clipboard } from '@ionic-native/clipboard';

export class Page {
    constructor(private clipboard: Clipboard) { }

    onCopy(text) {
        this.clipboard.copy(text);
    }

}
  • 1

    Works on Ionic 3.9.2?

  • When I used it was still in version 3, it should probably work.

  • Ionic 4 says copy is not part of the element

  • The library has changed, now it has a /ngx at the end. but now for this error: Typeerror: Object(...) is not a Function at Clipboard.copy (http://localhost:8100/build/vendor.js:79857:124)

-1

Use Ionic v3 for development, v4 version still has many bugs. When installing Ionic use the path: npm install [email protected] -g.

  • I’m using v3, it’s the same

Browser other questions tagged

You are not signed in. Login or sign up in order to post.