liveBook · Manning

Route parameter

As explained in Pro Angular 16

A route parameter is a value supplied through a URL at runtime and made available to the route's navigation or component logic.

A route parameter lets one Angular route accept different values in a URL segment. Write a colon followed by a name in the route path: `form/:mode` accepts both `form/create` and `form/edit`, assigning the second segment to `mode`. This can replace separate routes that target the same component. section 24.3.2

Use a URL value to select form behavior

Configure `{ path: "form/:mode", component: FormComponent }`. In the component, `activeRoute.snapshot.params["mode"] == "edit"` sets an editing flag. The component reads the value by name rather than parsing the URL itself. section 24.3.2

A parameter matches any value in its segment; it does not make the segment optional. Thus `form/london` matches `form/:mode`, but `form` has too few segments and `form/edit/1` has too many. A fixed segment must still match exactly. section 24.3.2

Route values can also populate component input properties when `bindToComponentInputs: true` is set in the router configuration. Read those inputs in `ngOnInit`, after route data has populated them. For an edit URL such as `form/edit/2`, add `:id`, generate the link with `[routerLink]="['/form', 'edit', item.id]"`, and convert the received ID string before looking up the product. section 24.3.2

The same pattern appears in child routes for an administration area, where parameters such as `:mode` and `:id` provide details that a selected component can use to change its behavior. section 7.5.2

How Pro Angular 16 covers it

How deep it goes
Works through matching, named retrieval, input binding, and dynamic links without examining router internals.
Examples
Uses create-and-edit form URLs, an edit link containing a product ID, and administration routes with mode and ID parameters.
What it assumes
Assumes Angular components and route configuration while defining parameter syntax and showing how to retrieve values.
What you can do after
Apply it via Angular Router to consolidate routes, read URL values, and populate a form for a selected product.
How it connects
Connects route configuration, URL matching, ActivatedRouteSnapshot, component inputs, routerLink, and child routes.

Also explained in

Compare how 6 books explain route parameter →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.