I have a shopping website made with Angular where you can either delete items from the same products page, or modify them by being sent to a form in another page, and then navigate back to products.
In both cases you have to manually reload the page to show the updated results, is there any way to avoid this and have it update dynamically?
These are the key pieces of code that i have right now:
Products page:
ngOnInit(): void {
this.productsService.getProducts().subscribe((res: any) => {
this.products = res;
})
}
edit(id: string){
this.productsService.getProductById(id).subscribe((res: any) => {
this.productsService.product = res;
this.router.navigate(['/edit']);
});
}
delete(id: string) {
this.productsService.deleteProduct(id).subscribe((res: any) => {
this.ngOnInit();
});
}
Products service:
getProducts(){
return this.http.get(this.url);
}
addProduct(newproduct: product){
return this.http.post(this.url, newproduct);
}
getProductById(id: string){
return this.http.get(this.url + '/' + id);
}
editProduct(newproduct: product){
return this.http.put(this.url + '/' + newproduct.id, newproduct);
}
deleteProduct(id: string){
return this.http.delete(this.url + '/' + id);
}
Edit page after you submit the new/edited product:
this.productRegisterForm.reset();
this.isSubmit = false;
this.router.navigate(['/products'])
I tried to use window.location.reload in many places (it would loop on the products page onInit, it wouldn't do anything on the Form page with a .then after the navigate and it wouldn't fetch the products correctly if added on the Products service (i wasn't able to in many cases)