Filtering Data Using Query Parameters

Learn how to use query parameters and prevent unauthorized access to a particular route using Angular guards.

In this lesson, we will learn about the optional query parameters. These parameters are considered optional because they aim to provide optional services such as sorting or filtering data. Some examples are as follows:

  • /products?sortOrder=asc: Sorts a list of products in ascending order

  • /products?page=3&pageSize=10: Splits a list of products into pages of 10 records and gets the third page

Query parameters are recognized in a route by the ? character. We can combine multiple query parameters by chaining them with an ampersand (&) character. The ActivatedRoute service contains a queryParamMap observable that we can subscribe to in order to get query parameter values. It returns a ParamMap object, similar to the paramMap observable, which we can query to get parameter values. For example, to retrieve the value of a sortOrder query parameter, we would use it as follows:

Get hands-on with 1200+ tech skills courses.