Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法从变量返回数据_Javascript_Angularjs_Ionic Framework_Chart.js_Ionic4 - Fatal编程技术网

Javascript 无法从变量返回数据

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

我正在使用chart.js创建图表。我这里有3个函数
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)
      }

请参见此处的更多信息

不太正确。顺便说一句,有了你的提示,我解决了我的问题,谢谢。。