Javascript 无法从变量返回数据
我正在使用chart.js创建图表。我这里有3个函数Javascript 无法从变量返回数据,javascript,angularjs,ionic-framework,chart.js,ionic4,Javascript,Angularjs,Ionic Framework,Chart.js,Ionic4,我正在使用chart.js创建图表。我这里有3个函数dataSurvey我用它从数据库返回数据,然后createBarChart生成图表,然后handle处理点击图表,我想在用户点击图表时显示一些信息 我的变量和加载函数 dataSurveys:any ionViewDidEnter() { this.dataSurvey() } 我的dataSurveycode dataSurvey() { this.api.get('product/getsurvey/'+thi
dataSurvey
我用它从数据库返回数据,然后createBarChart
生成图表,然后handle
处理点击图表,我想在用户点击图表时显示一些信息
我的变量和加载函数
dataSurveys:any
ionViewDidEnter() {
this.dataSurvey()
}
我的dataSurvey
code
dataSurvey() {
this.api.get('product/getsurvey/'+this.CekLogin.data.id)
.subscribe((result:any) => {
this.dataSurveys = result.data
if (this.dataSurveys) {
this.createBarChart()
}
})
}
createBarChart() {
this.bars = new Chart(this.barChart.nativeElement, {
type: 'pie',
data: {
labels: this.dataSurveys.prodName,
datasets: [{
data: this.dataSurveys.prodScore,
backgroundColor: this.dataSurveys.prodColor,
borderColor: 'rgb(38, 194, 129)',
borderWidth: 1
}]
},
options: {
onClick: this.handle
}
});
}
handle(point, event) {
let item = event[0]
console.log(item)
console.log(this.dataSurveys)
}
我的createBarChart
code
dataSurvey() {
this.api.get('product/getsurvey/'+this.CekLogin.data.id)
.subscribe((result:any) => {
this.dataSurveys = result.data
if (this.dataSurveys) {
this.createBarChart()
}
})
}
createBarChart() {
this.bars = new Chart(this.barChart.nativeElement, {
type: 'pie',
data: {
labels: this.dataSurveys.prodName,
datasets: [{
data: this.dataSurveys.prodScore,
backgroundColor: this.dataSurveys.prodColor,
borderColor: 'rgb(38, 194, 129)',
borderWidth: 1
}]
},
options: {
onClick: this.handle
}
});
}
handle(point, event) {
let item = event[0]
console.log(item)
console.log(this.dataSurveys)
}
和我的句柄
代码
dataSurvey() {
this.api.get('product/getsurvey/'+this.CekLogin.data.id)
.subscribe((result:any) => {
this.dataSurveys = result.data
if (this.dataSurveys) {
this.createBarChart()
}
})
}
createBarChart() {
this.bars = new Chart(this.barChart.nativeElement, {
type: 'pie',
data: {
labels: this.dataSurveys.prodName,
datasets: [{
data: this.dataSurveys.prodScore,
backgroundColor: this.dataSurveys.prodColor,
borderColor: 'rgb(38, 194, 129)',
borderWidth: 1
}]
},
options: {
onClick: this.handle
}
});
}
handle(point, event) {
let item = event[0]
console.log(item)
console.log(this.dataSurveys)
}
我在dataSurvey
和createBarChart
函数上尝试这个命令console.log(this.dataSurveys)
,它返回数据,但当我从handle
函数运行它时,它返回“未定义”
如何解决此问题?很可能
在handle
方法的上下文中,此
是另一回事。。。不是你的爱奥尼亚页面。您可以console.log(this)
并查看它实际上是什么
如果您确实需要使用页面中的变量逻辑,那么可以使用JS方法apply。它将基本上用您需要的上下文替换上下文
options: {
onClick: this.handle.apply(this)
}
请参见此处的更多信息不太正确。顺便说一句,有了你的提示,我解决了我的问题,谢谢。。