Ionic QR scanner

Hello,

I want a QR scanner in my ionic 2 app. I found this plugin but i don’t know how to import it after i run cordova add plugin xxxx.

Can someone help me with this?

Martijn

Once you’ve run the command you can just access the plugin directly, no need to import it or anything like that (you have to be running on a device though). So as it says in the docs:

   cordova.plugins.barcodeScanner.scan(
      function (result) {
          alert("We got a barcode\n" +
                "Result: " + result.text + "\n" +
                "Format: " + result.format + "\n" +
                "Cancelled: " + result.cancelled);
      }, 
      function (error) {
          alert("Scanning failed: " + error);
      }
   );

that will work anywhere in your code, or if you want to ES6 it up a bit:

   cordova.plugins.barcodeScanner.scan(
      (result) => {
          alert("We got a barcode\n" +
                "Result: " + result.text + "\n" +
                "Format: " + result.format + "\n" +
                "Cancelled: " + result.cancelled);
      }, 
      (error) => {
          alert("Scanning failed: " + error);
      }
   );

Thank you! this fixed my problem!

How can i set the result.text in this.url inside the function. Because the this is different in that function.

the this.url is connected with a input field

Use the ES6 syntax, it uses the parent scope this way so you don’t have to worry about any issues with this in callbacks.

the view is not updated. when I set the result in this.url nothing is happend but when i click on something then it is showing the value.

Is it posible to make this plugin call in a promise or Observables?

Because now he is not updating the this variable in the code

i want to know that where i call this “ionic plugin add cordova-plugin-geolocation” command in my ionic 2 project. from where i add cordova plugin in my ionic 2 project. i am using cordova in VS 2017. please help me.

You run that command on the command line in your project directory.

thanks sujan12 ? it hepls me lot ?