Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

311
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda