Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data is not updating on page after routing to same url with different params

Basically I have a notification component that show notification on click of icon in header. When we click the icon on header, it opens a popup and shows a list of notification. On clicking any one of the notification, it routes the user to the component of that particular notification, like on clicking notification , it takes user to that route : profile/guest/{guestid}/alerts/{alertid}. But when we click on another notification from the above mentioned route , it chnages the route params but does not reload the data of the new alert and shows the data of the old route only.

Note: data that are shown are fetched from route resolve. And as the component is already loaded , it doesn't call ngOnInit or constructor function when we click on another notification. But when we refresh the page , it show correct data according to the updated route.

I tried implementing different router config solution that reloads the resolves data. such as runGuardsAndResolvers along with onSameUrlNavigation.

I also tried calling the functions that sets data, in other angular component life cycle hooks such as ngAfterViewInit or ngAfterViewChecked.

I tried a few more solutions, but all of them were of no use.

 Notification.component.ts :-(inside header component in shared module)
/**
   * * Callback when user clicks on visit button in notification modal
   * @param notification : notification 
   */
  navigateToGuestOrCustomer(notification: notification) {
    let routePath =
      notification.model == ALERT_MODEL.GUEST ? "guest" : "customers";
    let routeParamId = notification.detail_id;
    let alertId = notification.id;
    this.router.navigate([`/profile/${routePath}/${routeParamId}/alerts/${alertId}`]);
  }


edit-alert.component.ts(inside profile module in guest component>alert component)

  ngOnInit() {
    this.fetchRouteData();
    this.setGuestId();
    this.setGuestName();
  }

  /**
   * * get guest name from route resolve
   */
  setGuestName(): void {
    let guestData = this.route.parent.snapshot.data["editGuestInfo"];
    this.guestName = guestData["name"];
  }

  formData: any;
  alertListForGuest = [];
  /**Fetch Data from Resolve */
  fetchRouteData() {
    this.formData = this.route.snapshot.data["alertDetailForGuest"];
    this.alertListForGuest = this.route.snapshot.data["alertListForGuest"];
    this.alertListForGuest.push(this.formData.alert);
  }

expected result : after clicking on other notification , route parameters should change and show the desired page with the updated data.

actual result : only the route is getting changed , not the data.

like image 965
Rohit Kumar Avatar asked Apr 16 '19 11:04

Rohit Kumar


1 Answers

ngOnInit might not be called again if only the route changes, but the component is not recreated. You should subscribe to the route changes and perform your fetch and update logic there

import { ActivatedRoute } from '@angular/router`
constructor(private activatedRoute : ActivatedRoute){  }

ngOnInit() {
    this.activatedRoute.url.subscribe(url =>{
        // Code to get the new notification data 
        // and display it
    });
}

In this way when you navigate to a new url, the code is execute again

like image 59
davideSerafini Avatar answered Oct 16 '22 14:10

davideSerafini