Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

315
Visualizações
How to store in Angular fuction return value into variable

I wanted to store the return value of an function into an variable. I tried this simple example.

drawChart(){
     var data = this.prepareData();
}

prepareData() {
    return 1;
}

But I got the error message:

Cannot read properties of undefined (reading 'prepareData')

Thank you for every help.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You have to call drawChart() method in ngOnInit().

So this method gets call and you can get the return value from the prepareData() method

Here I'm attaching stackblitz url for your ref : stackblitz code

Hope that solve your problem.

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to place drawChart() and prepareData() outside the ngInit(), constructor() and inside the component.

eg :

export class YourComponent implements OnInit {

   ngOnInit() {
     this.drawChart();
   }

   drawChart(){
       var data = this.prepareData();
    }

  prepareData() {
     return 1;
  }
 }

"this" keyword will point to the variables and functions inside the class, If you have declare these function inside a function, that won't work.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda