Undefined attribute after edit in subscribe

198 views Asked by At

First of all, sorry for this presentation, It is not on the same computer.

I am trying to hide/show a panel using a boolean attribute in angular. I got : public existsLegend: any = {}; and public isLegendPanelActive: boolean = false;

I coded a method getLegend(url): Observable<Blob> which uses http.get from httpClientModule. Then, I subscribed to this method to see the result/error. At this moment, i got :

this.getLegend(legendURL).subscribe((data) => {...}, (error) => {...}, () => {...})

in the success i do : this.existLegends[layer.layer.layerId] = true

in the error i do : this.existLegends[layer.layer.layerId] = false

And in Complete :

let toShow = false;
for(const key in existLegends){
  if(this.existLegends[key] == true){
    toShow = true;
    break;
  }
}
this.isPanelLegendActive = toShow;
console.log(this.isLegendPanelActive) // return true or false as needed

but If I put the same console.log outside the subscribe function I got console.log(this.isLegendPanelActive) // return undefined

I tried a lot of things to see if the value was undefined due to async subscribe. But it not seems to be that. In fact I used a setInterval to print each second the value of my var and it is still undefined. even at the first instance instead of false as initialization.

Please if someone has an idea and could help me. Thank you

0

There are 0 answers