我正在为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收到的项目?
答案 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");
});
}