Route Parameters in Angular: an overview
An address can carry a value: /categories/3 means category 3. One route with a parameter, categories/:categoryId, serves every category, like {id} in an ASP.NET Core route. It takes three steps: the route, the component that reads the value, and the links that put it in.
Journeyman · Angular · Updated
After this lesson you can
- Define a route with a parameter, such as categories/:categoryId
Key takeaways
- One route with a parameter, such as
categories/:categoryId, serves a page for every category: the address says which one. :categoryIdmatches exactly one part of the address:/categories/3, but not/categoriesor/categories/3/products.- A route parameter's value is always a string, such as
'3', because it comes from the address. categoriesandcategories/:categoryIdare different routes, and both can be in the list.
The categories page lists eight categories. Clicking one should open a page about that category. You can't write a route per category: there could be hundreds, and they change. Instead, the address carries the id:
http://localhost:4200/categories/3
means "the page for category 3", the same way GET /api/categories/3 asks your Web API for category 3.
The route
One route serves every category. Part of its path is a parameter, written with a colon:
{ path: 'categories/:categoryId', component: CategoryDetails, title: 'Category' },
:categoryId matches any one part of the address, and remembers it under the name categoryId:
| Address | Matches? | categoryId |
|---|---|---|
/categories/3 |
Yes | '3' |
/categories/8 |
Yes | '8' |
/categories |
No: there's no part for the parameter | |
/categories/3/products |
No: there's one part too many |
It's ASP.NET Core's [HttpGet("{id}")], with :categoryId in place of {id}.
The value is always a string, '3', because it comes from the address. The component turns it into a number.
In the list of routes
categories and categories/:categoryId are different routes, and both can be in the list: /categories shows the list, and /categories/3 shows one category.
export const routes: Routes = [
{ path: '', component: Home, title: 'Northwind' },
{ path: 'categories', component: CategoriesPage, title: 'Categories' },
{ path: 'categories/:categoryId', component: CategoryDetails, title: 'Category' },
{ path: 'products', component: ProductsPage, title: 'Products' },
{ path: '**', component: NotFound, title: 'Page not found' }
];
Three steps
A page for one category needs three things:
- The route, with the parameter: done, above.
- The component reads the parameter, and loads that category from the API: the next lesson.
- The links put the id into the address: the lesson after.
Do it: pick the category id out of an address
The route categories/:categoryId matches an address with exactly one part after categories, and remembers that part as categoryId. Write that matching yourself, in plain JavaScript.
The function gets one value:
address: the address after the host, starting with a slash ('/categories/3').
For example, '/categories/3':
| Step | Here |
|---|---|
| The parts between the slashes | categories, then 3 |
Two parts, and the first is categories? |
Yes |
categoryId |
'3', a string, because it comes from the address |
So categoryIdFrom('/categories/3') returns '3'.
Your job: write the function categoryIdFrom(address).
- When the address is
/categories/and one more part, return that part, as a string ('3'). - When there's no part after
categories('/categories'), returnnull. - When there's one part too many (
'/categories/3/products'), returnnull. - When the first part isn't
categories('/products/3'), returnnull.
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
One route with a :parameter serves a page for every item: the address says which one. Register it, read it in the component, and build links that fill it in.