2019-12-02 15:47:19 -05:00
# Dependency injection in Angular
2017-03-31 19:57:13 -04:00
2018-05-31 15:02:50 -04:00
Dependencies are services or objects that a class needs to perform its function.
2020-10-13 08:57:54 -04:00
Dependency injection, or DI, is a design pattern in which a class requests dependencies from external sources rather than creating them.
2017-03-31 19:57:13 -04:00
2020-10-13 08:57:54 -04:00
Angular's DI framework provides dependencies to a class upon instantiation.
You can use Angular DI to increase flexibility and modularity in your applications.
2017-02-22 13:09:39 -05:00
2018-10-17 08:58:11 -04:00
< div class = "alert is-helpful" >
2017-02-22 13:09:39 -05:00
2020-10-13 08:57:54 -04:00
See the < live-example > < / live-example > for a working example containing the code snippets in this guide.
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2020-10-13 08:57:54 -04:00
## Creating an injectable service
2017-02-22 13:09:39 -05:00
2020-10-13 08:57:54 -04:00
To generate a new `HeroService` class in the `src/app/heroes` folder use the following [Angular CLI ](cli ) command.
2017-02-22 13:09:39 -05:00
2021-05-07 06:28:39 -04:00
< code-example language = "sh" >
2018-05-31 15:02:50 -04:00
ng generate service heroes/hero
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2020-10-13 08:57:54 -04:00
This command creates the following default `HeroService` .
2017-03-31 19:57:13 -04:00
2018-10-11 07:29:59 -04:00
< code-example path = "dependency-injection/src/app/heroes/hero.service.0.ts" header = "src/app/heroes/hero.service.ts (CLI-generated)" >
2017-03-27 11:08:53 -04:00
< / code-example >
2020-10-13 08:57:54 -04:00
The `@Injectable()` decorator specifies that Angular can use this class in the DI system.
The metadata, `providedIn: 'root'` , means that the `HeroService` is visible throughout the application.
Next, to get the hero mock data, add a `getHeroes()` method that returns the heroes from `mock.heroes.ts` .
2017-02-22 13:09:39 -05:00
2018-10-11 07:29:59 -04:00
< code-example path = "dependency-injection/src/app/heroes/hero.service.3.ts" header = "src/app/heroes/hero.service.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2020-10-13 08:57:54 -04:00
For clarity and maintainability, it is recommended that you define components and services in separate files.
2017-02-22 13:09:39 -05:00
2020-10-13 08:57:54 -04:00
If you do combine a component and service in the same file, it is important to define the service first, and then the component.
If you define the component before the service, Angular returns a run-time null reference error.
2018-03-27 13:27:44 -04:00
2018-05-01 14:26:48 -04:00
2019-05-14 13:17:35 -04:00
{@a injector-config}
2018-03-27 13:27:44 -04:00
{@a bootstrap}
2018-05-31 15:02:50 -04:00
## Injecting services
2018-03-27 13:27:44 -04:00
2020-10-13 08:57:54 -04:00
Injecting services results in making them visible to a component.
2018-03-27 13:27:44 -04:00
2020-10-13 08:57:54 -04:00
To inject a dependency in a component's `constructor()` , supply a constructor argument with the dependency type.
The following example specifies the `HeroService` in the `HeroListComponent` constructor.
The type of `heroService` is `HeroService` .
2018-03-27 13:27:44 -04:00
2018-10-11 07:29:59 -04:00
< code-example header = "src/app/heroes/hero-list.component (constructor signature)" path = "dependency-injection/src/app/heroes/hero-list.component.ts"
2018-03-27 13:27:44 -04:00
region="ctor-signature">
< / code-example >
2021-01-23 09:14:11 -05:00
For more information, see [Providing dependencies in modules ](guide/providers ) and [Hierarchical injectors ](guide/hierarchical-dependency-injection ).
2018-03-27 13:27:44 -04:00
2020-10-13 08:57:54 -04:00
{@a service-needs-service}
2018-03-27 13:27:44 -04:00
2020-10-13 08:57:54 -04:00
## Using services in other services
2018-03-27 13:27:44 -04:00
2020-10-13 08:57:54 -04:00
When a service depends on another service, follow the same pattern as injecting into a component.
In the following example `HeroService` depends on a `Logger` service to report its activities.
2018-03-27 13:27:44 -04:00
2020-10-13 08:57:54 -04:00
First, import the `Logger` service.
Next, inject the `Logger` service in the `HeroService` `constructor()` by specifying `private logger: Logger` within the parentheses.
2018-03-27 13:27:44 -04:00
2020-10-13 08:57:54 -04:00
When you create a class whose `constructor()` has parameters, specify the type and metadata about those parameters so that Angular can inject the correct service.
2018-03-27 13:27:44 -04:00
2020-10-13 08:57:54 -04:00
Here, the `constructor()` specifies a type of `Logger` and stores the instance of `Logger` in a private field called `logger` .
2018-03-27 13:27:44 -04:00
2020-10-13 08:57:54 -04:00
The following code tabs feature the `Logger` service and two versions of `HeroService` .
The first version of `HeroService` does not depend on the `Logger` service.
The revised second version does depend on `Logger` service.
2018-03-27 13:27:44 -04:00
< code-tabs >
2018-10-11 07:29:59 -04:00
< code-pane header = "src/app/heroes/hero.service (v2)" path = "dependency-injection/src/app/heroes/hero.service.2.ts" >
2018-03-27 13:27:44 -04:00
< / code-pane >
2018-10-11 07:29:59 -04:00
< code-pane header = "src/app/heroes/hero.service (v1)" path = "dependency-injection/src/app/heroes/hero.service.1.ts" >
2018-03-27 13:27:44 -04:00
< / code-pane >
2018-10-11 07:29:59 -04:00
< code-pane header = "src/app/logger.service"
2018-05-31 15:02:50 -04:00
path="dependency-injection/src/app/logger.service.ts">
< / code-pane >
2018-03-27 13:27:44 -04:00
2018-05-31 15:02:50 -04:00
< / code-tabs >
2018-03-27 13:27:44 -04:00
2020-10-13 08:57:54 -04:00
In this example, the `getHeroes()` method uses the `Logger` service by logging a message when fetching heroes.
2017-03-31 19:57:13 -04:00
2020-10-13 08:57:54 -04:00
## What's next
2017-10-23 20:44:49 -04:00
2020-10-13 08:57:54 -04:00
* [Dependency providers ](guide/dependency-injection-providers )
* [DI tokens and providers ](guide/dependency-injection-providers )
* [Dependency Injection in Action ](guide/dependency-injection-in-action )