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.
This step adds some sample data to your new service. In a later lesson, you'll 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, from HomeComponent, copy the housingLocationList variable and its array value.
In src/app/housing.service.ts:
Inside the HousingService class, paste the variable that you copied from HomeComponent in the previous step.
Inside the HousingService class, 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 HousingLocation by id or the entire list.
Add a file level import for the HousingLocation.
import {HousingLocation} from './housinglocation';
Confirm that the app builds without error. Correct any errors before you continue to the next step.
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'll 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 the inject to the items imported from @angular/core. This will import the inject function into the HomeComponent class.
import {Component, inject} from '@angular/core';
Add a new file level import for the HousingService:
import {HousingService} from '../housing.service';
From HomeComponent, delete the housingLocationList array entries and assign housingLocationList the value of empty array ([]). In a few steps you will update the code to pull the data from the HousingService.
In HomeComponent, add the following code to inject the new service and initialize the data for the app. The constructor is the first function that runs when this component is created. The code in the constructor will assign the housingLocationList the value returned from the call to getAllHousingLocations.
Conceptual preview of services
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.
1. Create a new service for your app
This step creates an injectable service for your app. In the Terminal pane of your IDE:
first-app
directory, run this command to create the new service.ng serve
to build the app and serve it to http://localhost:4200.2. Add static data to the new service
This step adds some sample data to your new service. In a later lesson, you'll 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, from HomeComponent, copy the housingLocationList variable and its array value.
In src/app/housing.service.ts:
Inside the HousingService class, paste the variable that you copied from HomeComponent in the previous step.
Inside the HousingService class, 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 HousingLocation by 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.
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'll 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 the inject to the items imported from @angular/core. This will import the inject function into the HomeComponent class.
Add a new file level import for the HousingService:
From HomeComponent, delete the housingLocationList array entries and assign housingLocationList the value of empty array ([]). In a few steps you will update the code to pull the data from the HousingService.
In HomeComponent, add the following code to inject the new service and initialize the data for the app. The constructor is the first function that runs when this component is created. The code in the constructor will assign the housingLocationList the value returned from the call to getAllHousingLocations.
Save the changes to src/app/home/home.component.ts and confirm your app builds without error. Correct any errors before you continue to the next step.