Search code examples
javascriptcordovatypescriptionic2cordova-plugins

Creating Cordova plugin with Angular 2


I'm creating Cordova plugin for Ionic 2. I receive datas from Android, and show it in console or Alert, but I'm not able to show it on html view.

device: any[] = [];

constructor(public navCtrl: NavController, 
            private appService: AppService) {
}

ngOnInit(): void {
    devices_activity.devicesActivity(this.success, this.failure);
}

success(aL) {

    for (var i = aL.length - 1; i >= 0; i--) {
        aL[i];
        console.log('name: ' + aL[i]);
    }
}

failure() {
    alert("Error calling Devices Stone SDK Plugin");
}

I tried to put aL in device array, but I got an error: ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'device' of null

How can I show the items received from Android in html View?


Solution

  • I think you device variable is out of scope inside success function. Try to use this. I think this should resolve your issue.

    device: any[] = [];
    
    constructor(public navCtrl: NavController, 
                private appService: AppService) {
    }
    
    ngOnInit(): void {
      var scope = this;
      devices_activity.devicesActivity((aL: any) => {
        scope.device = aL;
      }, () => {
        alert("Error calling Devices Stone SDK Plugin");
      });
    }