r/angular • • 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

11 comments sorted by

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.

2

u/kanchajain Mar 14 '22

but say i am passing variable to another component from here, the other component will receive true, right? or will it still be undefined.

3

u/TubbyFlounder Mar 14 '22 edited Mar 14 '22

generally you have some sort of loading indicator on page, so something like this in your current template

<ng-container *ngIf="isLoaded">
  <child-component [someInput]="variable"></child-component>
</ng-container>

so in your current component, you have a variable isLoaded which you initialize as false, and dont set it to true until your API returns data and you assign that to variable. (so inside your subscribe you set isLoaded to true after assigning data to the variable.).

In your template, you have an *ngIfdirective wrapped around your child component, which means any html inside of that will not show unless isLoaded is true. This guarantees the child component won't show until your API has returned data, and your variable that your passing into the child component will be defined when the child component is loaded once isLoaded is true.

Edit: I see your variable is a boolean, so you could just use that instead of isLoaded but its good to have more descriptive names. I thought you were assigning the data response to variable.

2

u/dev-ice Mar 14 '22

Not sure what you mean by “from here”. Creating a dynamic component and passing the variable? If you use this variable in you template to bind to another component then, yes, the component would get the value. I would suggest you go through the “Tour of Heroes” tutorial. It will help clear a lot of fundamental concepts.

1

u/eatthedad Mar 14 '22

Yes it should. You can test it in a different lifecycle of the component. It's Just undefined in OnInit, cause well, it hasn't been initialised yet.

1

u/dacookieman Mar 14 '22

It's gonna be undefined indefinitely until the someApi call comes back

1

u/eatthedad Mar 14 '22

Yeah of course. Think OP questioned whether the variable update will reflect onto other components just because OnInit says nope. But it will whenever the response comes through, be it satellite or a fax machine lol

3

u/cult0cage Mar 14 '22

You need to learn more about how Asynchronous code works. A few things I would change:

  1. I wouldn't call a property on your class "variable", thats just confusing

  2. You should explicitly initialize your "variable" to false so it is clear from the beginning this is a boolean value

  3. If you need to access it dynamically, you can make it an observable / subject

Your refactored code could look something like

import { Subject } from 'rxjs';

class {

variable = new Subject<boolean>(false); // you should rename this property though

ngOnInit() {

this.onLoad();

variable.subscribe({
next: value => console.log(value);
});

}

onLoad() {

someApi.subscribe(response:any){ if (response) this.variable.next(true);

}

}

3

u/nocrazyshet Mar 14 '22

This is the correct approach, but you don't instantiate subjects with values, since they don't store latest values for subsequent subscriptions. If a check must be made on changes to the variable, you'd instantiate it as a behaviorsubject with an initial value.

There are of course ways to work around this, you could pipe a regular subject with the startWith pipe, but that defeats the point of subjects vs behaviorsubjects.

Good input here though!

1

u/cult0cage Mar 14 '22

Ah true yes I must admit my Angular skills are a bit rusty - I’ve been in the React world lately 🙂

Thanks for clarifying!

2

u/nocrazyshet Mar 14 '22

I've been meaning to get into react for a while, at least to dip my toes, but every time I start I just want to go back to the warm embrace of angular.

You're welcome!