From 0870af1740f58c76e8c8ccd8566bbe54f1c3fba2 Mon Sep 17 00:00:00 2001 From: Kapunahele Wong Date: Tue, 10 Nov 2020 14:25:12 -0500 Subject: [PATCH] docs: edit start-data.md (#39628) PR Close #39628 --- aio/content/start/start-data.md | 299 ++++++++++++-------------------- 1 file changed, 112 insertions(+), 187 deletions(-) diff --git a/aio/content/start/start-data.md b/aio/content/start/start-data.md index 6e1993c269..c39b5a11a2 100644 --- a/aio/content/start/start-data.md +++ b/aio/content/start/start-data.md @@ -1,57 +1,40 @@ # Managing data -At the end of [In-app Navigation](start/start-routing "Try it: In-app Navigation"), the online store application has a product catalog with two views: a product list and product details. +This guide builds on the second step of the [Getting started with a basic Angular application](start) tutorial, [Adding navigation](start/start-routing "Adding navigation"). +At this stage of development, the store application has a product catalog with two views: a product list and product details. Users can click on a product name from the list to see details in a new view, with a distinct URL, or route. -This page guides you through creating the shopping cart in three phases: +This step of the tutorial guides you through creating a shopping cart in the following phases: -* Update the product details view to include a "Buy" button, which adds the current product to a list of products that a cart service manages. +* Update the product details view to include a **Buy** button, which adds the current product to a list of products that a cart service manages. * Add a cart component, which displays the items in the cart. * Add a shipping component, which retrieves shipping prices for the items in the cart by using Angular's `HttpClient` to retrieve shipping data from a `.json` file. -{@a services} -## Services - -Services are an integral part of Angular applications. In Angular, a service is an instance of a class that you can make available to any part of your application using Angular's [dependency injection system](guide/glossary#dependency-injection-di "Dependency injection definition"). - -Services are the place where you share data between parts of your application. For the online store, the cart service is where you store your cart data and methods. - {@a create-cart-service} + ## Create the shopping cart service -Up to this point, users can view product information, and -simulate sharing and being notified about product changes. -They cannot, however, buy products. +In Angular, a service is an instance of a class that you can make available to any part of your application using Angular's [dependency injection system](guide/glossary#dependency-injection-di "Dependency injection definition"). -In this section, you add a "Buy" button to the product -details view and set up a cart service to store information -about products in the cart. +Currently, users can view product information, and the application can simulate sharing and notifications about product changes. -
- -A later part of this tutorial, [Use forms for user input](start/start-forms "Try it: Forms for user input"), guides you through accessing this cart service from the view where the user checks out. - -
+The next step is to build a way for users to add products to a cart. +This section walks you through adding a **Buy** button and setting up a cart service to store information about products in the cart. {@a generate-cart-service} + ### Define a cart service -1. To generate a cart service, right click on the `app` folder, choose `Angular Generator`, and choose `Service`. Name the new service `cart`. +1. To generate a cart service, right click on the `app` folder, choose **Angular Generator**, and choose **Service**. + Name the new service `cart`. - - -
- - The StackBlitz generator might provide the cart service in `app.module.ts` by default. That differs from the example, which uses a bundle-optimization technique, an `@Injectable()` decorator with the `{ providedIn: 'root' }` statement. - For more information about services, see [Introduction to Services and Dependency Injection](guide/architecture-services "Concepts > Intro to Services and DI"). - -
+ 1. In the `CartService` class, define an `items` property to store the array of the current products in the cart. -1. Define methods to add items to the cart, return cart items, and clear the cart items: +1. Define methods to add items to the cart, return cart items, and clear the cart items. @@ -59,105 +42,83 @@ A later part of this tutorial, [Use forms for user input](start/start-forms "Try * The `getItems()` method collects the items users add to the cart and returns each item with its associated quantity. - * The `clearCart()` method returns an empty array of items. + * The `clearCart()` method returns an empty array of items, which empties the cart. {@a product-details-use-cart-service} + ### Use the cart service -This section walks you through using the cart service to add a product to the cart with a "Buy" button. +This section walks you through using the `CartService` to add a product to the cart. -1. Open `product-details.component.ts`. +1. In `product-details.component.ts`, import the cart service. -1. Configure the component to use the cart service. + + - 1. Import the cart service. +1. Inject the cart service by adding it to the `constructor()`. - + - 1. Inject the cart service by adding it to the `constructor()`. - - - - - - 1. Define the `addToCart()` method, which adds the current product to the cart. - The `addToCart()` method does the following three things: - * Receives the current `product`. - * Uses the cart service's `addToCart()` method to add the product the cart. - * Displays a message that you've added a product to the cart. - -1. Update the product details template with a "Buy" button that adds the current product to the cart. + The `addToCart()` method does the following: + * Takes the current `product` as an argument. + * Uses the `CartService` `addToCart()` method to add the product the cart. + * Displays a message that you've added a product to the cart. - 1. Open `product-details.component.html`. +1. In `product-details.component.html`, add a button with the label **Buy**, and bind the `click()` event to the `addToCart()` method. + This code updates the product details template with a **Buy** button that adds the current product to the cart. - 1. Add a button with the label "Buy", and bind the `click()` event to the `addToCart()` method: + + - - + The line, `

{{ product.price | currency }}

`, 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"). -
- - The line, `

{{ product.price | currency }}

`, 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"). - -
- -1. To see the new "Buy" button, refresh the application and click on a product's name to display its details. +1. Verify that the new **Buy** button appears as expected by refreshing the application and clicking on a product's name to display its details. - 1. Click the "Buy" button to add the product to the stored list of items in the cart and display a confirmation message. + 1. Click the **Buy** button to add the product to the stored list of items in the cart and display a confirmation message. - ## Create the cart view -At this point, users can put items in the cart by clicking "Buy", but they can't yet see their cart. +For customers to see their cart, you can create the cart view in two steps: -Create the cart view in two steps: - -1. Create a cart component and configure routing to the new component. At this point, the cart view has only default text. +1. Create a cart component and configure routing to the new component. 1. Display the cart items. -### Set up the component +### Set up the cart component - To create the cart view, begin by following the same steps you did to create the product details component and configure routing for the new component. + To create the cart view, follow the same steps you did to create the `ProductDetailsComponent` and configure routing for the new component. -1. Generate a cart component, named `cart`. - - Reminder: In the file list, right-click the `app` folder, choose `Angular Generator` and `Component`. +1. Generate a cart component named `cart` by right-clicking the `app` folder, choosing **Angular Generator**, and **Component**. -1. Add routing (a URL pattern) for the cart component. - - Open `app.module.ts` and add a route for the component `CartComponent`, with a `path` of `cart`: +1. Open `app.module.ts` and add a route for the component `CartComponent`, with a `path` of `cart`. -1. Update the "Checkout" button so that it routes to the `/cart` url. +1. Update the **Checkout** button so that it routes to the `/cart` URL. + In `top-bar.component.html`, add a `routerLink` directive pointing to `/cart`. - Open `top-bar.component.html` and add a `routerLink` directive pointing to `/cart`. - - + -1. To see the new cart component, click the "Checkout" button. You can see the "cart works!" default text, and the URL has the pattern `https://getting-started.stackblitz.io/cart`, where `getting-started.stackblitz.io` may be different for your StackBlitz project. +1. Verify the new `CartComponent` works as expected by clicking the **Checkout** button. + You can see the "cart works!" default text, and the URL has the pattern `https://getting-started.stackblitz.io/cart`, where `getting-started.stackblitz.io` may be different for your StackBlitz project.