{ "id": "start/start-routing", "title": "Adding navigation", "contents": "\n\n\n
This guide builds on the first step of the Getting Started tutorial, Get started with a basic Angular app.
\nAt this stage of development, the online store application has a basic product catalog.
\nIn the following sections, you'll add the following features to the application:
\nThe application already uses the Angular Router
to navigate to the ProductListComponent
.\nThis section shows you how to define a route to show individual product details.
Generate a new component for product details.\nIn the file list, right-click the app
folder, choose Angular Generator
and Component
.\nName the component product-details
.
In app.module.ts
, add a route for product details, with a path
of products/:productId
and ProductDetailsComponent
for the component
.
Open product-list.component.html
.
Modify the product name anchor to include a routerLink
with the product.id
as a parameter.
The RouterLink
directive helps you customize the anchor element.\nIn this case, the route, or URL, contains one fixed segment, /products
.\nThe final segment is variable, inserting the id
property of the current product.\nFor example, the URL for a product with an id
of 1 would be similar to https://getting-started-myfork.stackblitz.io/products/1
.
Verify that the router works as intended by clicking the product name.\nThe application should display the ProductDetailsComponent
, which currently says \"product-details works!\"
Notice that the URL in the preview window changes.\nThe final segment is products/#
where #
is the number of the route you clicked.
The ProductDetailsComponent
handles the display of each product.\nThe Angular Router displays components based on the browser's URL and your defined routes.
In this section, you'll use the Angular Router to combine the products
data and route information to display the specific details for each product.
In product-details.component.ts
, import ActivatedRoute
from @angular/router
, and the products
array from ../products
.
Define the product
property.
Inject ActivatedRoute
into the constructor()
by adding private route: ActivatedRoute
as an argument within the constructor's parentheses.
ActivatedRoute
is specific to each component that the Angular Router loads.\nActivatedRoute
contains information about the route and the route's parameters.
By injecting ActivatedRoute
, you are configuring the component to use a service.\nThe Managing Data step covers services in more detail.
In the ngOnInit()
method, extract the productId
from the route parameters and find the corresponding product in the products
array.
The route parameters correspond to the path variables you define in the route.\nTo access the route parameters, we use route.snapshot
, which is the ActivatedRouteSnapshot
that contains information about the active route at that particular moment in time.\nThe URL that matches the route provides the productId
.\nAngular uses the productId
to display the details for each unique product.
Update the ProductDetailsComponent
template to display product details with an *ngIf
.\nIf a product exists, the <div>
renders with a name, price, and description.
The line, <h4>{{ product.price | currency }}</h4>
, uses the currency
pipe to transform product.price
from a number to a currency string.\nA pipe is a way you can transform data in your HTML template.\nFor more information about Angular pipes, see Pipes.
When users click on a name in the product list, the router navigates them to the distinct URL for the product, shows the ProductDetailsComponent
, and displays the product details.
For more information about the Angular Router, see Routing & Navigation.
\nYou have configured your application so you can view product details, each with a distinct URL.
\nTo continue exploring Angular:
\n