I have home.ts which is calling service.ts to load items from Storage.
export class HomePage {
products;
ionViewDidEnter() {
this.products = this.productService.products;
}
ngOnInit(){
this.productService.fetchProducts();
this.products = this.productService.products;
}
}
export class ProductService{
products;
fetchProducts(){
this.storage.get('products') // returns a promise which returns data or error
.then(
(products) => {
// assign to this.expenses only if not null. When first //strt, can be null. If null, assign empty array []
products? this.products = products : this.products = [];
console.log("fetch Products:" + this.products);
return this.products;
})
.catch(
err => console.log(err)
);
}
I then render the items in home.html
.
The problem is, the items don't get displayed the first time round when the app starts. But if I navigate to another screen and return back to home.ts, the items get rendered back fine. I understand that this is because of ionViewDidEnter and perhaps the first time round, the promise in fetchProducts is asynchronous. But how do I get the items to list the first time round on ngOnInit
?