Wednesday, 22 January 2020

Angular - Refresh or Re Subscribe service when route gets changed

I have one component in my application in which products get called through service. I Call this service through a function on click method. When I click I pass a parameter to get a certain category. Now my problem is when I change my route, the list is not getting refreshed.

Service.ts

getProducts(service): Observable<Product[]>{
    return this.db.list("/products/" + service)
}

products.ts (component)

Products: Product[] = [];
filteredProducts: Product[] = [];
urlArea : string;
urlService: string;

constructor(
    private route: ActivatedRoute,
    private ProductService: ProductsService) {
      this.urlArea = this.route.snapshot.paramMap.get('area');
      this.urlService = this.route.snapshot.paramMap.get('service');
  }
private populateProducts(){
    this.ProductService
    .getProducts(this.urlService)
    .subscribe(Products => {
      this.filteredProducts = Products; // all listings will be stored in this
      this.Products = Products;
    });
}

I pass a parameter to service through route parameter. It works fine but when the route gets to change it doesn't refresh the list. I need to refresh the page to view the updated list.



from Angular - Refresh or Re Subscribe service when route gets changed

No comments:

Post a Comment