{ "id": "start/start-data", "title": "Managing data", "contents": "\n\n\n
This guide builds on the second step of the Getting started with a basic Angular application tutorial, Adding navigation.\nAt this stage of development, the store application has a product catalog with two views: a product list and product details.\nUsers can click on a product name from the list to see details in a new view, with a distinct URL, or route.
\nThis step of the tutorial guides you through creating a shopping cart in the following phases:
\nHttpClient
to retrieve shipping data from a .json
file.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.
\nCurrently, users can view product information, and the application can simulate sharing and notifications about product changes.
\nThe next step is to build a way for users to add products to a cart.\nThis section walks you through adding a Buy button and setting up a cart service to store information about products in the cart.
\n\nTo generate a cart service, right click on the app
folder, choose Angular Generator, and choose Service.\nName the new service cart
.
In the CartService
class, define an items
property to store the array of the current products in the cart.
Define methods to add items to the cart, return cart items, and clear the cart items.
\nThe addToCart()
method appends a product to an array of items
.
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, which empties the cart.
This section walks you through using the CartService
to add a product to the cart.
In product-details.component.ts
, import the cart service.
Inject the cart service by adding it to the constructor()
.
Define the addToCart()
method, which adds the current product to the cart.
The addToCart()
method does the following:
product
as an argument.CartService
addToCart()
method to add the product to the cart.In product-details.component.html
, add a button with the label Buy, and bind the click()
event to the addToCart()
method.\nThis code updates the product details template with a Buy button that adds the current product to the cart.
Verify that the new Buy button appears as expected by refreshing the application and clicking on a product's name to display its details.
\nClick the Buy button to add the product to the stored list of items in the cart and display a confirmation message.
\nFor customers to see their cart, you can create the cart view in two steps:
\n To create the cart view, follow the same steps you did to create the ProductDetailsComponent
and configure routing for the new component.
Generate a cart component named cart
by right-clicking the app
folder, choosing Angular Generator, and Component.
StackBlitz also generates an ngOnInit()
by default in components. You can ignore the CartComponent
ngOnInit()
for this tutorial.
Open app.module.ts
and add a route for the component CartComponent
, with a path
of cart
.
Update the Checkout button so that it routes to the /cart
URL.\nIn top-bar.component.html
, add a routerLink
directive pointing to /cart
.
Verify the new CartComponent
works as expected by clicking the Checkout button.\nYou 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.
This section shows you how to use the cart service to display the products in the cart.
\nIn cart.component.ts
, import the CartService
from the cart.service.ts
file.
Inject the CartService
so that the CartComponent
can use it by adding it to the constructor()
.
Define the items
property to store the products in the cart.
This code sets the items using the CartService
getItems()
method.\nYou defined this method when you created cart.service.ts
.
Update the cart template with a header, and use a <div>
with an *ngFor
to display each of the cart items with its name and price.\nThe resulting CartComponent
template is as follows.
Verify that your cart works as expected:
\nFor more information about services, see Intro to Services and DI\">Introduction to Services and Dependency Injection.
\nServers often return data in the form of a stream.\nStreams are useful because they make it easy to transform the returned data and make modifications to the way you request that data.\nAngular HttpClient
is a built-in way to fetch data from external APIs and provide them to your application as a stream.
This section shows you how to use HttpClient
to retrieve shipping prices from an external file.
The application that StackBlitz generates for this guide comes with predefined shipping data in assets/shipping.json
.\nUse this data to add shipping prices for items in the cart.
AppModule
to use HttpClient
linkTo use Angular's HttpClient
, you must configure your application to use HttpClientModule
.
Angular's HttpClientModule
registers the providers your application needs to use the HttpClient
service throughout your application.
In app.module.ts
, import HttpClientModule
from the @angular/common/http
package at the top of the file with the other imports.\nAs there are a number of other imports, this code snippet omits them for brevity.\nBe sure to leave the existing imports in place.
To register Angular's HttpClient
providers globally, add HttpClientModule
to the AppModule
@NgModule()
imports
array.
CartService
to use HttpClient
linkThe next step is to inject the HttpClient
service into your service so your application can fetch data and interact with external APIs and resources.
In cart.service.ts
, import HttpClient
from the @angular/common/http
package.
Inject HttpClient
into the CartService
constructor()
.
CartService
to get shipping priceslinkTo get shipping data, from shipping.json
, You can use the HttpClient
get()
method.
In cart.service.ts
, below the clearCart()
method, define a new getShippingPrices()
method that uses the HttpClient
get()
method.
For more information about Angular's HttpClient
, see the Client-Server Interaction guide.
Now that you've configured your application to retrieve shipping data, you can create a place to render that data.
\nGenerate a new component named shipping
by right-clicking the app
folder, choosing Angular Generator, and selecting Component.
In app.module.ts
, add a route for shipping.\nSpecify a path
of shipping
and a component of ShippingComponent
.
There's no link to the new shipping component yet, but you can see its template in the preview pane by entering the URL its route specifies.\nThe URL has the pattern: https://getting-started.stackblitz.io/shipping
where the getting-started.stackblitz.io
part may be different for your StackBlitz project.
ShippingComponent
to use CartService
linkThis section guides you through modifying the ShippingComponent
to retrieve shipping data via HTTP from the shipping.json
file.
In shipping.component.ts
, import CartService
.
Inject the cart service in the ShippingComponent
constructor()
.
Define a shippingCosts
property that sets the shippingCosts
property using the getShippingPrices()
method from the CartService
.
Update the ShippingComponent
template to display the shipping types and prices using the async
pipe.
The async
pipe returns the latest value from a stream of data and continues to do so for the life of a given component.\nWhen Angular destroys that component, the async
pipe automatically stops.\nFor detailed information about the async
pipe, see the AsyncPipe API documentation.
Add a link from the CartComponent
view to the ShippingComponent
view.
Click the Checkout button to see the updated cart.\nRemember that changing the application causes the preview to refresh, which empties the cart.
\nClick on the link to navigate to the shipping prices.
\nYou now have a store application with a product catalog, a shopping cart, and you can look up shipping prices.
\nTo continue exploring Angular:
\n