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

183
Views
Fetching variables & calling functions from different scope in Angular

I have a local function say, compulsoryFunction() inside ngOnInit() which is triggered automatically by a library I'm using. I am bound to have it as a local function inside ngOnInit.

Problem - I am unable to access variables or call functions declared in that angular Component.

    export class MyComponent implements OnInit {
    message= "Hello";
    OuterFunction(){
      console.log("Outer Function called");
    }
    ngOnInit(): void { 
    console.log('msg0: ',this.message);  
    function  compulsoryFunction() {
    console.log('msg: ',this.message);
    this.OuterFunction();
    }

The error is as follows:

enter image description here

Are variables or function defined in the component not public by default?

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can change the this context using arrow functions.

export class MyComponent implements OnInit {
  message = 'Hello';

  OuterFunction() {
    console.log('Outer Function called');
  }

  ngOnInit() {
    console.log('msg0: ', this.message);

    const compulsoryFunction = () => {
      console.log('msg: ', this.message);
      this.OuterFunction();
    };
  }
}
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!