By Alligator and Bradley Kouchi

Query parameters in Angular allow for passing optional parameters across any route in the application. Query parameters are different from regular route parameters, which are only available on one route and are not optional (e.g., /product/:id).
In this article, you will reference an example of an application that displays a list of products. You will provide optional order and price-range values that the receiving component can read and act on. The values provided will affect the ordering and filtering of the list of products.
To follow through this tutorial, you’ll need:
Router and RouterLink and ActivatedRoute may be beneficial.Router.navigateIf you are navigating to the route imperatively using Router.navigate, you will pass in query parameters with queryParams.
In our example, if we want to route visitors to the products with the list ordered by popularity, it would look like this:
goProducts() {
this.router.navigate(
['/products'],
{ queryParams: { order: 'popular' } }
);
}
This will result in a URL that resembles:
http://localhost:4200/products?order=popular
You can also provide multiple query parameters. In our example, if we want to route visitors to the products with the list ordered by popularity and filtered with an expensive price range, it would look like this:
goProducts() {
this.router.navigate(
['/products'],
{ queryParams: { order: 'popular', 'price-range': 'not-cheap' } }
);
}
This will result in a URL that resembles:
http://localhost:4200/products?order=popular&price-range=not-cheap
Now, you have an understanding of how queryParams can be used to set query parameters.
queryParamsHandlingBy default, the query parameters are lost on any subsequent navigation action. To prevent this, you can set queryParamsHandling to either 'preserve' or 'merge'.
In our example, if we want to route visitors from a page with the query parameter { order: 'popular' } to the /users page while keeping the query parameters, we would use 'preserve':
goUsers() {
this.router.navigate(
['/users'],
{ queryParamsHandling: 'preserve' }
);
}
This will result in a URL that resembles:
http://localhost:4200/users?order=popular
In our example, if we want to route visitors from a page with the query parameter { order: 'popular' } to the /users page while passing the 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
Note: Preserving query parameters used to be achieved with preserveQueryParams set to true, but this is now deprecated in Angular 4+ in favor of queryParamsHandling.
Now, you have an understanding of how queryParamsHandling can be used to preserve and merge query parameters.
RouterLinkIn our example, if instead, you are using the RouterLink directive to navigate to the route, you would use queryParams like this:
<a
[routerLink]="['/products']"
[queryParams]="{ order: 'popular'}"
>
Products
</a>
And in our example, if you want to 'preserve' or 'merge' query parameters on subsequent navigation you would use queryParamsHandling like this:
<a
[routerLink]="['/users']"
[queryParams]="{ filter: 'new' }"
queryParamsHandling="merge"
>
Users
</a>
Now you understand how queryParams and queryParamsHandling can be used with RouterLink.
Now that we know how to pass in optional query parameters to a route, let’s see how to access these values on the resulting routes. The ActivatedRoute class has a queryParams property that returns an observable of the query parameters that are available in the current URL.
Given the following route URL:
http://localhost:4200/products?order=popular
We can access the order query parameter like this:
// ...
import { ActivatedRoute } from '@angular/router';
import 'rxjs/add/operator/filter';
@Component({ ... })
export class ProductComponent implements OnInit {
order: string;
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.route.queryParams
.filter(params => params.order)
.subscribe(params => {
console.log(params); // { order: "popular" }
this.order = params.order;
console.log(this.order); // popular
}
);
}
}
In the console log, we would see the params object:
Output{ order: "popular" }
And the params.order value:
Outputpopular
There’s also queryParamMap, which returns an observable with a paramMap object.
Given the following route URL:
http://localhost:4200/products?order=popular&filter=new
We can access the query parameters like this:
this.route.queryParamMap
.subscribe((params) => {
this.orderObj = { ...params.keys, ...params };
}
);
We used the object spread operator here, and this is the resulting shape of the data in orderObj:
{
"0": "order",
"1": "filter",
"params": {
"order": "popular",
"filter": "new"
}
}
Now, you have an understanding of how queryParams and queryParamMap can be used to access values on the resulting routes.
In this article, you used different examples to set and get query parameters in Angular. You were introduced to queryParams and queryParamsHandling with Router.navigate and RouterLink. You were also introduced to queryParams and queryParamMap with ActivatedRoute.
If you’d like to learn more about Angular, check out our Angular topic page for exercises and programming projects.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Alligator.io is a developer-focused resource that offers tutorials and insights on a wide range of modern front-end technologies, including Angular 2+, Vue.js, React, TypeScript, Ionic, and JavaScript.
Former Technical Editor at DigitalOcean. Expertise in areas including Vue.js, CSS, React, and more.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.