0
0
Angularframework~10 mins

How dependency injection works in Angular - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - How dependency injection works in Angular
Component or Service needs a dependency
Angular checks the injector tree
Find provider for the dependency
Create instance if not created
Inject instance into the requester
Component/Service uses the dependency
Angular looks for the requested dependency in its injector tree, creates it if needed, and provides it to the component or service that asked for it.
Execution Sample
Angular
import { Injectable, inject } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class LoggerService {
  log(msg: string) { console.log(msg); }
}

export class AppComponent {
  private logger = inject(LoggerService);
  constructor() { this.logger.log('App started'); }
}
This code shows Angular injecting LoggerService into AppComponent using the inject() function.
Execution Table
StepActionInjector StateDependency FoundInstance CreatedInjected Into
1AppComponent requests LoggerServiceEmptyNoNoNo
2Angular checks root injectorEmptyYesNoNo
3LoggerService instance createdLoggerService instance createdYesYesNo
4LoggerService instance injected into AppComponentLoggerService instance createdYesYesAppComponent
5AppComponent calls logger.log()LoggerService instance createdYesYesAppComponent
💡 Dependency injected and used; execution stops after AppComponent construction.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
LoggerService instanceundefinedcreatedcreatedcreated
AppComponent.loggerundefinedundefinedLoggerService instanceLoggerService instance
Key Moments - 3 Insights
Why does Angular create only one instance of LoggerService?
Because LoggerService is provided in 'root', Angular creates a singleton instance in the root injector as shown in execution_table step 3.
What happens if the dependency is not found in the injector?
Angular throws an error because it cannot provide the requested dependency, but in this example, the dependency is found at step 2.
How does Angular know where to inject the dependency?
Angular uses the inject() function or constructor parameters to know where to inject, demonstrated in step 4 where the instance is injected into AppComponent.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the LoggerService instance created?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Check the 'Instance Created' column in the execution_table.
According to the variable tracker, when does AppComponent.logger get assigned the LoggerService instance?
AAfter Step 4
BAfter Step 3
CAfter Step 2
DAt Start
💡 Hint
Look at the 'AppComponent.logger' row in variable_tracker after Step 4.
If LoggerService was not provided in 'root', what would happen during injection?
AAngular would create a new instance anyway
BAngular would inject null
CAngular would throw an error at step 2
DAngular would skip injection
💡 Hint
Refer to key_moments about what happens if dependency is not found.
Concept Snapshot
Angular Dependency Injection (DI):
- Components or services request dependencies.
- Angular checks injector tree for providers.
- Creates singleton instances if needed.
- Injects instances where requested.
- Use @Injectable and inject() for DI.
- Root injector provides app-wide singletons.
Full Transcript
Dependency injection in Angular works by components or services asking for dependencies. Angular looks into its injector tree to find a provider for the requested dependency. If the provider exists and no instance is created yet, Angular creates one. Then Angular injects this instance into the requester. For example, a LoggerService marked with @Injectable({providedIn: 'root'}) is created once in the root injector. When AppComponent uses inject(LoggerService), Angular provides the same LoggerService instance. This process ensures efficient reuse and easy testing. If Angular cannot find a provider, it throws an error. This flow is shown step-by-step in the execution table and variable tracker.