konstantinkrassmann / ngCordova-nfc

Wrapper for the NFC functionality
MIT License
11 stars 4 forks source link

NFC CORDOVA NOT WORKING IN ANGULARJS #12

Open pradipta007 opened 5 years ago

pradipta007 commented 5 years ago

I am trying to implement(phonegap-nfc) plugin into my angularJS project but it is not working.

app.controller('homeController',['$scope','$http','$rootScope','$cordovaNfc','$cordovaNfcUtil',function($scope,$http,$rs,$cordovaNfc,$cordovaNfcUtil)

{
    $cordovaNfc.then(function(nfcInstance){
        nfcInstance.addNdefListener(function(event){
            //Callback when ndef got triggered
            var tag = JSON.stringify(event.tag);

        if (tag.serialNumber) {
          $scope.tagid = tag.serialNumber;
          tag.isWritable = !tag.isLocked;
          tag.canMakeReadOnly = tag.isLockable;
          alert("Hello"+$scope.tagid);
        }

          })
        .then(
        //Success callback
        function(event){
          alert("bound success");
        },
        //Fail callback
        function(err){
          alert("error");
        });
      });

  $cordovaNfcUtil.then(function(nfcUtil){
    alert( nfcUtil.bytesToString("some bytes") );
  });

But the nfc plugin is working in plain Javascript project.
Here is the working JS code

var app = {
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function() {
        // this.receivedEvent('deviceready');

        nfc.addTagDiscoveredListener
        nfc.addTagDiscoveredListener(app.onNfc, // tag successfully scanned 
        function (status) {    // listener successfully initialized
        app.display("Tap a tag to read its id number.");
        },
        function (error) {    // listener fails to initialize
        app.display("NFC reader failed to initialize " +JSON.stringify(error));
        }
        );
    },
    onNfc: function(nfcEvent){
    var tag = nfcEvent.tag;
    app.display("Read tag: " + nfc.bytesToHexString(tag.id));
    },

    /*   appends @message to the message div:*/
        display: function(message) {
            var label = document.createTextNode(message),
            lineBreak = document.createElement("br");
            messageDiv.appendChild(lineBreak);// add a line break
            messageDiv.appendChild(label);// add the text
        },

    // clears the message div
    clear: function() {
        messageDiv.innerHTML = "";
    },