Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

314
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!