In my angular project, I have the following setup:
I have API to fetch some data from server at the beginning of the app load:
ItemFetch.ts
At the beginning of app load, it fetches data from API then changes the itemLoaded to true.
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
...
dataLoaded: boolean = false;
dataAPI(){ //Stores in local storage
...
.then(data=>{
this.itemLoaded = true;
})
...
}
main.ts:
Then, once data is stored, I need to load the stored data only when itemLoaded from ItemFetch.ts is true.
import { dataFromStorage} from './data_from_storage' //local storage
export class main_page {
constructor(public itemStorage: dataFromStorage){};
ngOnInit(){
this.fetchInitialData();
}
//Fetch the data from the storage
fetchInitialData(){
this.itemStorage.GetItemDataFromStorage('some_item_id')
.then((data) => {
console.log("Got the data!" + data);
)
};
}
Question:
How do I share this dataLoaded from one component to another so that I can initiate this.fetchInitialData(); only when dataLoaded is true?