I have encountered an issue in Angular 2 where a variable is returning undefined. The problem arises when a function calls a service to initialize a variable, which is then used in another function to make a get HTTP request. However, the get request fails because the variable is undefined.
Here is the code snippet:
variable:any;
constructor(private http: Http, private serviceOne: ServiceOne){
function1(); //service call to get data
function2(); //http get request
}
function1(){
calls request //subscribes data
data =>{this.variable = data.var};
}
function2(){
console.log(this.variable); //undefined
http get request uses this.variable
}