Does it possible to run ngAfterViewInit again?

1.2k views Asked by At

I want to re-call my ngAfterViewInit since it is responsible for changing my dom's style. So if my components data changes my component gets updated but my styles are not applying on the new data. Here in the below code my component which is responsible for showing some product information is subscribed to router params observable. So if I change the route params than information gets changed without rerunning my component but styles are not applied since component is not initiated again so afterViewInit is not called again. Here my code

ngOnInit() {
    this.currentRoute.params.subscribe((params) => {
      this.productId = params['productId'];
      this.category = params['category'];
      this.productService.getSingleProduct(this.category, this.productId); //fetching product data
    });
  }
ngAfterViewInit(){
   // some codes to change the dom styles
}

So how I can re-call my ngAfterViewInit if I get new sets of product data?

**Important points----

1- I can't put my ngAfterViewInit code to params subscription because that code depends on dynamic contents.

2- I know I can call ngAfterViewInit method in ngAfterViewChecked lifecycle hook which works just fine but I think I am calling those functions too often.

0

There are 0 answers