Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 24c0897

Browse filesBrowse files
kapunahelewongthePunderWoman
authored andcommitted
docs: edit step 2 of getting started start-routing.md (#39593)
PR Close #39593
1 parent 288a0a6 commit 24c0897
Copy full SHA for 24c0897

File tree

Expand file treeCollapse file tree

2 files changed

+58
-58
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+58
-58
lines changed

‎aio/content/examples/getting-started/src/app/product-details/product-details.component.1.ts

Copy file name to clipboardExpand all lines: aio/content/examples/getting-started/src/app/product-details/product-details.component.1.ts
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import { products } from '../products';
1111
templateUrl: './product-details.component.html',
1212
styleUrls: ['./product-details.component.css']
1313
})
14-
// #docregion props-methods, add-to-cart
14+
// #docregion props-methods, add-to-cart, product-prop
1515
export class ProductDetailsComponent implements OnInit {
1616
product;
17+
// #enddocregion product-prop
1718

1819
constructor(
1920
private route: ActivatedRoute,
@@ -25,7 +26,9 @@ export class ProductDetailsComponent implements OnInit {
2526
this.route.paramMap.subscribe(params => {
2627
this.product = products[+params.get('productId')];
2728
});
29+
// #docregion product-prop
2830
}
31+
// #enddocregion product-prop
2932
// #enddocregion get-product
3033
// #docregion props-methods
3134
}

‎aio/content/start/start-routing.md

Copy file name to clipboard
+54-57Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,83 @@
11
# Adding navigation
22

3-
At the end of [part 1](start "Get started with a basic Angular app"), the online store application has a basic product catalog.
4-
The app doesn't have any variable states or navigation.
5-
There is one URL, and that URL always displays the "My Store" page with a list of products and their descriptions.
3+
This guide builds on the first step of the Getting Started tutorial, [Get started with a basic Angular app](start "Get started with a basic Angular app").
64

7-
This guide shows you how to use Angular [routing](guide/glossary#router "Router definition") to give the user in-app navigation. In a single-page app, instead of loading new pages, you show different components and data to the user based on where the user is in the application.
5+
At this stage of development, the online store application has a basic product catalog.
86

9-
The router lets you display full product details in separate [views](guide/glossary#view "View definition"), each with its own URL. Routing enables navigation from one view to the next (within the same page) as users perform tasks such as the following:
7+
In the following sections, you'll add the following features to the application:
108

11-
* Entering a URL in the address bar to navigate to a corresponding view.
12-
* Clicking links on the page to navigate to a new view.
13-
* Clicking the browser's back and forward buttons to navigate backward and forward through the browser history.
9+
* Type a URL in the address bar to navigate to a corresponding product page.
10+
* Click links on the page to navigate within your single-page application.
11+
* Click the browser's back and forward buttons to navigate the browser history intuitively.
1412

15-
## Registering a route
13+
{@a define-routes}
1614

17-
The app is already set up to use the Angular `Router` and to use routing to navigate to the product list component you modified earlier. This section shows you how to define a route to show individual product details.
15+
## Associate a URL path with a component
1816

19-
1. Generate a new component for product details. Give the component the name `product-details`.
17+
The application already uses the Angular `Router` to navigate to the `ProductListComponent`.
18+
This section shows you how to define a route to show individual product details.
2019

21-
Reminder: In the file list, right-click the `app` folder, choose `Angular Generator` and `Component`.
20+
1. Generate a new component for product details.
21+
In the file list, right-click the `app` folder, choose `Angular Generator` and `Component`.
22+
Name the component `product-details`.
2223

2324
1. In `app.module.ts`, add a route for product details, with a `path` of `products/:productId` and `ProductDetailsComponent` for the `component`.
2425

2526
<code-example header="src/app/app.module.ts" path="getting-started/src/app/app.module.ts" region="product-details-route">
2627
</code-example>
2728

28-
A route associates one or more URL paths with a component.
29+
1. Open `product-list.component.html`.
2930

30-
1. The directive configures the component template to define how the user navigates to the route or URL. When the user clicks a product name, the app displays the details for that product.
31+
1. Update the `*ngFor` directive to read as follows.
32+
This statement instructs Angular to iterate over the items in the `products` array and assigns each index in the array to the `productId` variable when iterating over the list.
3133

32-
1. Open `product-list.component.html`.
33-
34-
1. Update the `*ngFor` directive to assign each index in the `products` array to the `productId` variable when iterating over the list.
35-
36-
1. Modify the product name anchor to include a `routerLink`.
34+
1. Modify the product name anchor to include a `routerLink`.
3735

3836
<code-example header="src/app/product-list/product-list.component.html" path="getting-started/src/app/product-list/product-list.component.html" region="router-link">
3937
</code-example>
4038

41-
The RouterLink directive gives the router control over the anchor element. In this case, the route, or URL, contains one fixed segment, `/products`, while the final segment is variable, inserting the id property of the current product. For example, the URL for a product with an `id` of 1 will be similar to `https://getting-started-myfork.stackblitz.io/products/1`.
39+
The `RouterLink` directive helps you customize the anchor element.
40+
In this case, the route, or URL, contains one fixed segment, `/products`.
41+
The final segment is variable, inserting the `id` property of the current product.
42+
For example, the URL for a product with an `id` of 1 would be similar to `https://getting-started-myfork.stackblitz.io/products/1`.
4243

43-
1. Test the router by clicking a product name. The app displays the product details component, which currently always says "product-details works!"
44+
1. Verify that the router works as intended by clicking the product name.
45+
The application should display the `ProductDetailsComponent`, which currently says "product-details works!"
4446

45-
Notice that the URL in the preview window changes. The final segment is `products/#` where `#` is the number of the route you clicked.
47+
Notice that the URL in the preview window changes.
48+
The final segment is `products/#` where `#` is the number of the route you clicked.
4649

4750
<div class="lightbox">
4851
<img src="generated/images/guide/start/product-details-works.png" alt="Product details view with updated URL">
4952
</div>
5053

54+
## View product details
5155

56+
The `ProductDetailsComponent` handles the display of each product.
57+
The Angular Router displays components based on the browser's URL and [your defined routes](#define-routes).
5258

53-
## Using route information
54-
55-
The product details component handles the display of each product. The Angular Router displays components based on the browser's URL and your defined routes. This section shows you how to use the Angular Router to combine the `products` data and route information to display the specific details for each product.
56-
57-
1. Open `product-details.component.ts`
59+
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.
5860

59-
1. Arrange to use product data from an external file.
61+
1. In `product-details.component.ts`, import `ActivatedRoute` from `@angular/router`, and the `products` array from `../products`.
6062

61-
1. Import `ActivatedRoute` from the `@angular/router` package, and the `products` array from `../products`.
63+
<code-example header="src/app/product-details/product-details.component.ts" path="getting-started/src/app/product-details/product-details.component.1.ts" region="imports">
64+
</code-example>
6265

63-
<code-example header="src/app/product-details/product-details.component.ts" path="getting-started/src/app/product-details/product-details.component.1.ts" region="imports">
64-
</code-example>
66+
1. Define the `product` property.
6567

66-
1. Define the `product` property and inject the `ActivatedRoute` into the constructor by adding it as an argument within the constructor's parentheses.
68+
<code-example header="src/app/product-details/product-details.component.ts" path="getting-started/src/app/product-details/product-details.component.1.ts" region="product-prop">
69+
</code-example>
6770

68-
<code-example header="src/app/product-details/product-details.component.ts" path="getting-started/src/app/product-details/product-details.component.1.ts" region="props-methods">
69-
</code-example>
71+
1. Inject `ActivatedRoute` into the `constructor()` by adding `private route: ActivatedRoute` as an argument within the constructor's parentheses.
7072

71-
The `ActivatedRoute` is specific to each routed component that the Angular Router loads. It contains information about the
72-
route, its parameters, and additional data associated with the route.
73+
<code-example header="src/app/product-details/product-details.component.ts" path="getting-started/src/app/product-details/product-details.component.1.ts" region="props-methods">
74+
</code-example>
7375

74-
By injecting the `ActivatedRoute`, you are configuring the component to use a *service*. The [Managing Data](start/start-data "Try it: Managing Data") page covers services in more detail.
76+
`ActivatedRoute` is specific to each component that the Angular Router loads.
77+
`ActivatedRoute` contains information about the route and the route's parameters.
7578

79+
By injecting `ActivatedRoute`, you are configuring the component to use a service.
80+
The [Managing Data](start/start-data "Try it: Managing Data") step covers services in more detail.
7681

7782
1. In the `ngOnInit()` method, subscribe to route parameters and fetch the product based on the `productId`.
7883

@@ -81,39 +86,31 @@ The product details component handles the display of each product. The Angular R
8186

8287
The route parameters correspond to the path variables you define in the route. The URL that matches the route provides the `productId`. Angular uses the `productId` to display the details for each unique product.
8388

84-
1. Update the template to display product details information inside an `*ngIf`.
89+
1. Update the `ProductDetailsComponent` template to display product details with an `*ngIf`.
90+
If a product exists, the `<div>` renders with a name, price, and description.
8591

8692
<code-example header="src/app/product-details/product-details.component.html" path="getting-started/src/app/product-details/product-details.component.html" region="details">
8793
</code-example>
8894

89-
<div class="alert is-helpful">
95+
The line, `<h4>{{ product.price | currency }}</h4>`, uses the `currency` pipe to transform `product.price` from a number to a currency string.
96+
A pipe is a way you can transform data in your HTML template.
97+
For more information about Angular pipes, see [Pipes](guide/pipes "Pipes").
9098

91-
The line, `<h4>{{ product.price | currency }}</h4>`, uses the `currency` pipe to transform `product.price` from a number to a currency string. A pipe is a way you can transform data in your HTML template. For more information about Angular pipes, see [Pipes](guide/pipes "Pipes").
92-
93-
</div>
94-
95-
Now, when users click on a name in the product list, the router navigates them to the distinct URL for the product, swaps out the product list component for the product details component, and displays the product details.
99+
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.
96100

97101
<div class="lightbox">
98102
<img src="generated/images/guide/start/product-details-routed.png" alt="Product details page with updated URL and full details displayed">
99103
</div>
100104

101-
102-
103-
<div class="alert is-helpful">
104-
105105
For more information about the Angular Router, see [Routing & Navigation](guide/router "Routing & Navigation guide").
106106

107-
</div>
108-
107+
<hr />
109108

110-
## Next steps
109+
## What's next
111110

112-
Congratulations! You have integrated routing into your online store.
111+
You have configured your application so you can view product details, each with a distinct URL.
113112

114-
* Products are linked from the product list view to individual products.
115-
* Users can click on a product name from the list to see details in a new view, with a distinct URL/route.
113+
To continue exploring Angular:
116114

117-
To continue exploring Angular, choose either of the following options:
118-
* [Continue to the "Managing Data" section](start/start-data "Try it: Managing Data") to add a shopping cart feature, use a service to manage the cart data and use HTTP to retrieve external data for shipping prices.
119-
* [Skip ahead to the Deployment section](start/start-deployment "Try it: Deployment") to deploy your app to Firebase or move to local development.
115+
* Continue to [Managing Data](start/start-data "Try it: Managing Data") to add a shopping cart feature, manage cart data, and retrieve external data for shipping prices.
116+
* Skip ahead to [Deployment](start/start-deployment "Try it: Deployment") to deploy your application to Firebase or move to local development.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.