In my component, I am using the ngAfterViewInit lifecycle hook to handle certain tasks:
ngAfterViewInit() {
this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 0);
this.subscription = this.dataService.dataChanged
.subscribe(
() => {
this.getData();
}
);
this.getData();
}
I am facing an issue where I want the subscription logic to run before the this.getData()
outside the subscription block. If for some reason the subscription does not run, then I would like the this.getData()
outside the subscription to execute.
The challenge is that placing this logic in ngOnInit does not work as expected due to the paginated nature of the table. The subscription needs to be triggered when new data is added to the table so that it can be displayed automatically to the user.
Updated Scenario: Currently, when a user navigates to the page, the initial data is loaded onto the paginated table correctly. Subsequently, if the user adds a new item via a form on another element and returns to the table view, the previous behavior loads the table initially without the newly added item, requiring a reload to display all data including the addition. Ideally, I would prefer that upon navigating back, the updated data is instantly visible to the user without the need for a manual refresh.