
If you’re prepping for an Angular interview, expect to be asked about Dependency Injection (DI). Itβs a core design pattern in Angular β and knowing how it works can set you apart from other candidates.
In this post, Iβll cover must-know DI interview questions, with clear examples and answers that will help you not only pass the interview β but understand Angular at a deeper level.
Letβs jump in π
β 1. What Is Dependency Injection in Angular?
Answer:
Dependency Injection is a pattern that allows Angular to provide required services to a class instead of that class creating them itself. This keeps things clean, testable, and decoupled.
Angular comes with its own injector system that resolves and injects dependencies (like services) into components, directives, pipes, etc.
constructor(private userService: UserService) {}
β 2. How Does Angular Implement DI?
Answer:
Angular uses the @Injectable()
decorator to mark a class as eligible for DI.
DI in Angular follows constructor injection, meaning Angular injects the required dependencies via the class constructor.
@Injectable()
export class UserService {
constructor(private http: HttpClient) {}
}
β
3. What Is @Injectable()
and Why Is It Important?
Answer:@Injectable()
tells Angular that the class can be injected as a dependency. It also lets Angular generate the metadata it needs to manage DI behind the scenes.
@Injectable({
providedIn: 'root' // Makes this a singleton service
})
export class AuthService {}
β 4. What Are the Different Provider Scopes?
Answer:
providedIn: 'root'
β Singleton for the entire app- Module-level (legacy) β Scoped to a specific module
- Component-level β New instance for each component
Example of a component-level provider:
@Component({
selector: 'app-child',
providers: [LoggerService] // A fresh instance just for this component
})
export class ChildComponent {}
β
5. What’s the Difference Between providedIn: 'root'
and Component-Level Providers?
Answer:
providedIn: 'root'
: You get a shared singleton instance across the app.- Component-level provider: A new instance is created every time the component is rendered.
Use component-level when you need isolation β like managing separate form states or logs per component.
β 6. What Is Angularβs Hierarchical Injector?
Answer:
Angular builds a tree of injectors, starting from the root. Each component can have its own child injector.
If a dependency isn’t found locally, Angular moves up the tree to find it.
π‘ This allows you to override a service instance at any level in the tree.
β 7. Can You Inject an Interface in Angular?
Answer:
No β interfaces donβt exist at runtime, so Angular can’t inject them directly.
Instead, use an InjectionToken
:
export const LOGGER = new InjectionToken<Logger>('Logger');
@NgModule({
providers: [{ provide: LOGGER, useClass: ConsoleLogger }]
})
Inject it like this:
constructor(@Inject(LOGGER) private logger: Logger) {}
β 8. How Do You Mock a Service for Unit Testing?
Answer:
You can mock services using Angularβs TestBed
and useValue
or useClass
.
const mockService = {
getData: () => of(['mocked data'])
};
TestBed.configureTestingModule({
providers: [{ provide: DataService, useValue: mockService }]
});
This keeps tests isolated and predictable without calling real APIs.
β
9. Explain useClass
, useValue
, useExisting
, and useFactory
.
Option | What It Does |
---|---|
useClass | Provide a specific class |
useValue | Provide a literal value or object |
useExisting | Use another tokenβs instance |
useFactory | Return a value/service via a function |
Examples:
{ provide: API_URL, useValue: 'https://api.example.com' }
{ provide: LoggerService, useClass: CustomLogger }
{ provide: NewLogger, useExisting: LoggerService }
{ provide: ConfigService, useFactory: () => new Config(env) }
β 10. Common DI Pitfalls to Watch Out For
- β Overusing
providedIn: 'root'
when scoped services are more appropriate - β Forgetting
@Injectable()
on services that need DI - β Creating circular dependencies between services
- β Injecting services into services without considering scope
- β Using global singleton services for data that should be component-local
π― Bonus Pro Tips for Angular Interviews
- Understand the lifecycle of services in the DI hierarchy
- Be able to write and explain
InjectionToken
use cases - Show how to test services with mocks using
TestBed
- Learn how standalone components handle DI (introduced in Angular 15+)
- Practice configuring providers with
useFactory
,useValue
, etc.
π§ Final Thoughts
Dependency Injection isnβt just a technical topic β itβs foundational to how Angular apps scale and stay maintainable.
Whether you’re aiming for a junior role or a senior Angular developer position, understanding DI deeply will not only help you ace interviews β itβll help you write better apps.
π¬ Have a follow-up question? Want more Angular interview prep like this?
Drop a comment or follow β Iβve got more deep dives coming! π