首页 > 解决方案 > 如何访问函数范围之外的变量Angular 9

问题描述

我正在学习角度,我正在尝试访问范围之外的变量值。我试过使用this. 没用,帮帮我

export class DashboardComponent   {
  private population: number;
  private country: string;
  constructor(private dummyService: DummyService) {
    this.myData();
  }

  myData(){
     this.dummyService.getList().subscribe((data) => {
      this.response = data;
        this.population = this.response.map(data => data.population);
        this.country = this.response.map(data => data.country);
        //able to console it here

    });
    //not able to console it here
  }

     chart1 = {
      data: {
        labels: this.country,// want to access it here
        datasets: [{
          label: 'population',
          data: this.population, // want to access it here
          backgroundColor: 'transparent',
          borderColor: '#5b6582',
          borderWidth: 2
        },
        ]
      },
      options: {
        scales: {
          yAxes: [{
            ticks: {
              fontColor: 'rgba(0,0,0,.6)',
              fontStyle: 'bold',
              beginAtZero: true,
              maxTicksLimit: 8,
              padding: 10
            }
          }]
        },
        responsive: true,
        legend: {
          position: 'bottom',
          display: false
        },
      }
    };

ngOnInit() {

    new Chart('chart-line', {
      type: 'line',
      data: this.chart1.data,
      options: this.chart1.options
    });
  }

标签: javascriptangulartypescript

解决方案


您应该在获取数据方法之前创建变量chart1并创建initChart()和调用。

 export class DashboardComponent   {
      private population: number;
      private country: string;
      chart1 : any;
      constructor(private dummyService: DummyService) {

        this.myData();
      }

      myData(){
         this.dummyService.getList().subscribe((data) => {
          this.response = data;
            this.population = this.response.map(data => data.population);
            this.country = this.response.map(data => data.country);
            //able to console it here
            this.initChart();
        });
        //not able to console it here
      }

      initChart(){
        let seft = this;
         chart1 = {
          data: {
            labels: self.country,// want to access it here
            datasets: [{
              label: 'population',
              data: self.population, // want to access it here
              backgroundColor: 'transparent',
              borderColor: '#5b6582',
              borderWidth: 2
            },
            ]
          },
          options: {
            scales: {
              yAxes: [{
                ticks: {
                  fontColor: 'rgba(0,0,0,.6)',
                  fontStyle: 'bold',
                  beginAtZero: true,
                  maxTicksLimit: 8,
                  padding: 10
                }
              }]
            },
            responsive: true,
            legend: {
              position: 'bottom',
              display: false
            },
          }
        };
     }

ngOnInit() {

    new Chart('chart-line', {
      type: 'line',
      data: this.chart1.data,
      options: this.chart1.options
    });
  }

推荐阅读