0

I have the following component, but I can't print test variable value inside onClick function of Chart.

export class EntradaSaidaComponent {

  @ViewChild('graficoDeBarraTemplate')
  private graficoDeBarraTemplate;
  graficoDeBarra: Chart;
  test:any = "Test";

  ngOnInit(){
    this.graficoDeBarra = new Chart(this.graficoDeBarraTemplate.nativeElement, {
    type: 'bar',
    data: {
      labels: ["Entrada", "Saída"],
      datasets: [
        {
          backgroundColor: ["#00b050", "#00b0f0"],
          data: [this.dashboard.totalReceber, this.dashboard.totalPagar],
          label: this.sharedProvider.formatMoeda(this.dashboard.totalReceber)
        },
        {
          backgroundColor: ["#03A9F4"],
          data: [],
          label: this.sharedProvider.formatMoeda(this.dashboard.totalPagar)
        },
      ]
    },
    options: {
      responsive: true,
      plugins: {
      },
      legend: {
        display: true,
        position: 'bottom'
      },
      onClick: function(){
        console.log(this.test);
      },
    },
  });
  }

}

How to solve it?

2 Answers 2

2

Use the arrow function

onClick: () => {
    console.log(this.test);
  },
Sign up to request clarification or add additional context in comments.

Comments

1

Context issue.

Either (yes, this is a strange syntax for an object, but trust me it works)

onClick(){
    console.log(this.test);
  },

Or

onClick: () => console.log(this.test),

5 Comments

Thank you man, but I need to accept only one and David answered some seconds before
@renanzin No problem, I'm here to help, not for the reputation (and as you can see I have plenty) !
man how to acess event and array of click chart js inside this function? for example chartClickEvent(event, array) it works but I can't acess the component variable
@renanzin sorry, you'll have to be more specific, I don't get what you want there
I will ask a new question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.