Query Parameters in Angular: an overview
Values that are optional, such as a filter, a search or a page number, go in the query string: /products?categoryId=2&page=3. They need no route of their own, and the page shows with or without them. A route parameter says which item; a query parameter says how to show the page.
Journeyman · Angular · Updated
After this lesson you can
- Choose between a route parameter and a query parameter for a value in the address
Key takeaways
- A route parameter says which item the page is about; a query parameter is optional and changes how the page shows, such as a filter or a page number.
- Query parameters go after a
?as name=value pairs, separated by&:/products?categoryId=2&page=3. - Query parameters need no route of their own: the
productsroute matches/productswith or without them. - To choose, take the value away: if the page still makes sense with a default, it's a query parameter.
A route parameter is required: /categories/:categoryId never matches /categories, so the details page can't show without an id. That's right for "which category?". But some values are optional.
Optional values
The products page shows every product. It could also show:
- only one category's products,
- page 3 of the list,
- the products matching a search.
Without any of those, it should still show, with sensible defaults: all categories, page 1, no search. Those values go in the query string, after a ?:
http://localhost:4200/products
http://localhost:4200/products?categoryId=2
http://localhost:4200/products?categoryId=2&page=3
It's the same query string your Web API reads into an optional parameter, GetAll(int? categoryId), for GET /api/products?categoryId=2. Each value is a name=value pair, and & separates them.
No new route
Query parameters don't need a route. The path is still /products, so the products route you already have matches all three addresses above, and shows ProductsPage each time. The page reads the query parameters, and uses defaults for any that are missing: the next lesson.
Which one?
| Use | When | Example |
|---|---|---|
| A route parameter | The value says which thing the page is about, and the page makes no sense without it | /categories/3, /orders/10248 |
| A query parameter | The value is optional, and changes how the page shows: a filter, a sort, a search, a page number | /products?categoryId=2&page=3 |
It's the same choice as between route values and the query string in ASP.NET Core MVC. A good test: take the value away. If the page still makes sense, with a default, it's a query parameter. If it doesn't, it's a route parameter.
Both kinds live in the address, so they can be bookmarked and shared: send someone /products?categoryId=8, and they see the seafood.
Do it: build the products page's address
The products page has two optional values: a category, and a page number. Write a function, in plain JavaScript, that puts the ones it's given into the address's query string, and leaves the rest out.
The function gets two values:
categoryId: the category to show, a number (2), orundefinedfor all categories.page: the page number (3), orundefinedfor the first page.
For example, category 2, page 3:
| Value | Goes in the address as |
|---|---|
categoryId is 2 |
categoryId=2 |
page is 3 |
page=3 |
Both, after /products |
/products?categoryId=2&page=3 |
So productsAddress(2, 3) returns '/products?categoryId=2&page=3'.
Your job: write the function productsAddress(categoryId, page), which returns the address as a string.
- Start with
/products. - Add each value that isn't
undefinedas aname=valuepair:categoryIdfirst, thenpage. - Put
?before the first pair, and&between two pairs. - When both are
undefined, the address is/products, with no?.
Practiced live on the lesson page: write the JavaScript right there and check it.
Check yourself
1 question to try on the lesson page.
So what
Route parameters say which item; query parameters say how to show the page. Optional values go after the ?, need no route, and always have a default.