使用Angular 2创建Cordova插件

时间:2017-04-17 21:32:34

标签: javascript cordova typescript ionic2 cordova-plugins

我正在为Ionic 2创建Cordova插件。我从Android接收数据,并在控制台或警报中显示,但我无法在html视图中显示它。

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");
}

我尝试将aL放入device数组中,但收到错误:ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'device' of null

如何在html视图中显示从Android收到的项目?

1 个答案:

答案 0 :(得分:1)

我认为device变量超出success函数范围。试着用这个。我认为这应该可以解决您的问题。

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");
  });
}