无法将 Cordova 插件返回值分配给 Ionic2 中的视图
Cannot assign Cordova plugin returned value to views in Ionic2
在我的 ionic2 应用程序中,我尝试使用 Ionic Native 中未包含的 Cordova 插件。我在这里参考了 Josh Morony 写的文章:https://www.joshmorony.com/using-cordova-plugins-in-ionic-2-with-ionic-native/。一切正常(代码编译、执行没有问题)。
由于Ionic Native不支持此插件,所以没有promise和observable,需要一段时间才能回复。这个插件确实 return 值。我试图将插件中的 returned 值分配给我的视图,但它失败了(错误消息:TypeError:null 不是对象(评估 'this.name=t'))。
PS:如果我只是将响应放在警报中,警报(响应),它会正确显示 returned 值。
下面是我的代码,这里有人能帮帮我吗?非常感谢:-)
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Platform } from 'ionic-angular';
declare var airwatch;
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
name: string = null;
constructor(public navCtrl: NavController, platform: Platform) {
// code starts here
platform.ready().then(() => {
(<any>window).plugins.airwatch.username(function(response){
alert(response);// this alert works! it will correctly show the response in text mode (the response is text)
this.name = response; // the line failed, it said: cannot insert null to this.name but I thought the response is text and it works when I use alert or console.log
}, function(error){
this.name = error;
});
});
}
}
我也试过把代码放到ionViewDidLoad(){}里,还是不行。同样的错误信息:TypeError: null is not an object (evaluating 'this.name=t')
我试过 NgZone,但没用。
是不是returned的值不在angular"scope"中?
您需要保留对 'this' 的引用,因为回调中的第二个 'this' 是相对于回调的。
所以只需添加
let self = this;
在调用插件之前,然后在回调中您可以使用新创建的 'self' 变量。你的代码看起来像
let self = this;
(<any>window).plugins.airwatch.username(function(response){
alert(response);// this alert works! it will correctly show the response in text mode (the response is text).
this.name = response; // the line failed, it said: cannot insert null to self.name but I thought the response is text and it works when I use alert or console.log
}, function(error){
self.name = error;
});
在我的 ionic2 应用程序中,我尝试使用 Ionic Native 中未包含的 Cordova 插件。我在这里参考了 Josh Morony 写的文章:https://www.joshmorony.com/using-cordova-plugins-in-ionic-2-with-ionic-native/。一切正常(代码编译、执行没有问题)。
由于Ionic Native不支持此插件,所以没有promise和observable,需要一段时间才能回复。这个插件确实 return 值。我试图将插件中的 returned 值分配给我的视图,但它失败了(错误消息:TypeError:null 不是对象(评估 'this.name=t'))。
PS:如果我只是将响应放在警报中,警报(响应),它会正确显示 returned 值。
下面是我的代码,这里有人能帮帮我吗?非常感谢:-)
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Platform } from 'ionic-angular';
declare var airwatch;
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
name: string = null;
constructor(public navCtrl: NavController, platform: Platform) {
// code starts here
platform.ready().then(() => {
(<any>window).plugins.airwatch.username(function(response){
alert(response);// this alert works! it will correctly show the response in text mode (the response is text)
this.name = response; // the line failed, it said: cannot insert null to this.name but I thought the response is text and it works when I use alert or console.log
}, function(error){
this.name = error;
});
});
}
}
我也试过把代码放到ionViewDidLoad(){}里,还是不行。同样的错误信息:TypeError: null is not an object (evaluating 'this.name=t')
我试过 NgZone,但没用。
是不是returned的值不在angular"scope"中?
您需要保留对 'this' 的引用,因为回调中的第二个 'this' 是相对于回调的。
所以只需添加
let self = this;
在调用插件之前,然后在回调中您可以使用新创建的 'self' 变量。你的代码看起来像
let self = this;
(<any>window).plugins.airwatch.username(function(response){
alert(response);// this alert works! it will correctly show the response in text mode (the response is text).
this.name = response; // the line failed, it said: cannot insert null to self.name but I thought the response is text and it works when I use alert or console.log
}, function(error){
self.name = error;
});