Angular Js

How to Preserve and Merge Query String Values?

How to Preserve and Merge Query String Values?

How to Preserve and Merge Query String Values?

This Article helps you to Understand, How to Preserve and Merge Query String Values?


Preserving Query Parameters   

By default, the query parameters are lost on any subsequent navigation action. To prevent this, you can set queryParamsHandling to either 'preserve' or 'merge'.


Lets navigate to Product page with popular products

goProducts() {
  this.router.navigate(
    ['/products'],
    { queryParams: { order: 'popular' } }
  );
}




Now if we want to route visitors from a product page with the query parameter { order: 'popular' } to the /users page while keeping the query parameters, in-tact

goUsers() {
  this.router.navigate(
    ['/users'],
    { queryParamsHandling: 'preserve' }
  );
}



This will result in a URL that resembles:
http://localhost:4200/users?order=popular





Merging Query Parameters  
Now if we want to route visitors from a product page with the query parameter { order: 'popular' } to the /users page and while moving we want to merge a new query parameter { filter: 'new' }, we would use 'merge':

goUsers() {
  this.router.navigate(
    ['/users'],
    {
      queryParams: { filter: 'new' },
      queryParamsHandling: 'merge' }
    );
}


This will result in a URL that resembles:
http://localhost:4200/users?order=popular&filter=new



Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.

Instagram