Search code examples
angulartypescriptangular-routingangular-routerquery-string

ANGULAR - Defining Query Parameteres on the Back button press


I want to add query parameters (for the previous page) on the back button press in the browser.

For example, program code for adding query parameters while calling a customer-hierarchy component view:

<a [routerLink]="['/customer-hierarchy']" [queryParams]="{filterBy: currentActiveFilter}">Details</a>

So, how to add query parameters for the previous page which is triggered by clicking the default back button in the browser?


Solution

  • I resolved this problem with session storage. After every filter update on the table, I store these filters in session storage with this line of code:

    sessionStorage.setItem('workordersTableFilters', JSON.stringify(this.currentActiveFilter));
    

    If I want to load data from session storage:

    const workordersTableFilters = sessionStorage.getItem('workordersTableFilters');
    if (workordersTableFilters != null) {
       //Use loaded data
    }
    

    PS. There is no external library or import necessary