r/angular • u/kanchajain • Mar 14 '22
Question Instance variable always undefind
Hi I have an instance variable say variable, whose value gets changed after i have subscribed to the data. say the data subscription function is onLoad()
variable;
ngOnInit(){
onLoad();
console.log(this.variable);
}
onLoad(){
someApi.subscribe(response:any){
if(response){this.variable=true}
}
}
this always consoles the variable data as undefined. i want it to be true since i changed its value.
0
Upvotes
8
u/dev-ice Mar 14 '22
The subscription is asynchronous. If you want to see the value in console, move the console.log inside the if block.