I'm using RxJS subscribe to get data from my service, and then pass it to another function.
Everything works well but when I try to assign the same data to a public variable to use it somewhere, it returns undefined...
This is my public variable.
public prices: Array<any> = [];
This is my function:
getPriceData(book){
//MarketService
this.marketService.getPricesByBook(book).subscribe(data => {
this.prices = data.payload; // I want to assign the payload to a public variable to use later...
//ChartingService
this.price_data = this.chartService.getAveragePriceDataByTime(this.prices, this.interval);
}, err => {
console.log(err);
return false;
});
console.log("Outside subscribe : ", this.prices); // Throws undefined
}
Note that I want to reduce API calls since the data gets loaded at first (ngOnInit), I just want to let the user modify the data that's already loaded...
I'm open to suggestions on caching and/or saving the data to localStorage (if that's the case)