Add content for lesson 11. Update previous lessons and code examples be in line with the latest lesson formatting. PR Close #49980
6.8 KiB
First Angular app lesson 09 - Angular services
This tutorial lesson demonstrates how to create an Angular service and use dependency injection to include it in your app.
Time required: expect to spend about 15 minutes to complete this lesson.
Before you start
This lesson starts with the code from the previous lesson, so you can:
- Use the code that you created in Lesson 8 in your integrated development environment (IDE).
- Start with the code example from the previous lesson. Choose the from Lesson 8 where you can:
- Use the live example in StackBlitz, where the StackBlitz interface is your IDE.
- Use the download example and open it in your IDE.
If you haven't reviewed the introduction, visit the Introduction to Angular tutorial to make sure you have everything you need to complete this lesson.
If you have any trouble during this lesson, you can review the completed code for this lesson, in the for this lesson.
After you finish
- Your app has a service to serve the data to your app. At the end of this lesson, the service reads data from local, static data. In a later lesson, you update the service to get data from a web service.
Conceptual preview of interfaces
This tutorial introduces Angular services and dependency injection.
Angular services
Angular services provide a way for you to separate Angular app data and functions that can be used by multiple components in your app. To be used by multiple components, a service must be made injectable. Services that are injectable and used by a component become dependencies of that component. The component depends on those services and can't function without them.
Dependency injection
Dependency injection is the mechanism that manages the dependencies of an app's components and the services that other components can use.
Lesson steps
Perform these steps on the app code in your IDE.
Step 1 - Create a new service for your app
This step creates an injectable service for your app.
In the Terminal pane of your IDE:
-
In your project directory, navigate to the
first-appdirectory. -
In the
first-appdirectory, run this command to create the new service.ng generate service housing --skip-tests
-
Run
ng serveto build the app and serve it tohttp://localhost:4200. -
Confirm that the app builds without error. Correct any errors before you continue to the next step.
Step 2 - Add static data to the new service
This step adds some sample data to your new service.
In a later lesson, you replace the static data with a web interface to get data as you might in a real app.
For now, your app's new service uses the data that has, so far, been created locally in HomeComponent.
In the Edit pane of your IDE:
-
In
src/app/home/home.component.ts, fromHomeComponent, copy thehousingLocationListvariable and its array value. -
In
src/app/housing.servivce.ts:-
Inside the
HousingServiceclass, paste the variable that you copied fromHomeComponentin the previous step. -
Inside the
HousingServiceclass, paste these functions after the data you just copied. These functions allow dependencies to access the service's data.You will need these functions in a future lesson. For now, it is enough to understand that these functions return either a specific
HousingLocationby id or the entire list. -
Add a file level import for the
HousingLocation.
-
-
Confirm that the app builds without error. Correct any errors before you continue to the next step.
Step 3 - Inject the new service into HomeComponent
This step injects the new service into your app's HomeComponent so that it can read the app's data from a service.
In a later lesson, you replace the static data with a live data source to get data as you might in a real app.
In the Edit pane of your IDE, in src/app/home/home.component.ts:
-
At the top of
src/app/home/home.component.ts, add theinjectto the items imported from@angular/common. This will import theinjectfunction into theHomeComponentclass. -
Add a new file level import for the
HousingService: -
From
HomeComponent, delete thehousingLocationListdelete the array entries and assignhousingLocationListthe value of empty array ([]). In a few steps you will update the code to pull the data from theHousingService. -
In
HomeComponent, add this code to inject the new service and initialize the data for the app. Theconstructoris the first function that runs when this component is created. The code in theconstructorwill assign thehousingLocationListthe value returned from the call togetAllHousingLocations. -
Save the changes to
src/app/home/home.component.tsand confirm your app builds without error. Correct any errors before you continue to the next step.
Lesson review
In this lesson, you added an Angular service to your app and injected it into the HomeComponent class.
This compartmentalizes how your app gets its data.
For now, the new service gets its data from a static array of data.
In a later lesson, you refactor the service to get its data from a from an API endpoint.
If you are having any trouble with this lesson, you can review the completed code for it in the .
Next steps
More information
For more information about the topics covered in this lesson, visit: