Dependency Injection in Angular is a design pattern where Angular automatically provides components or services with the dependencies they need, instead of creating them manually.
Click to reveal answer
intermediate
How does Angular know which dependency to inject?
Angular uses tokens, usually class types, to look up and provide the correct dependency from its injector tree.
Click to reveal answer
intermediate
What is an Angular injector?
An injector is a container that holds and manages the creation and lifetime of dependencies. Angular creates a tree of injectors to provide dependencies at different levels.
Click to reveal answer
beginner
What is the role of the @Injectable() decorator in Angular DI?
The @Injectable() decorator marks a class as available to be injected as a dependency and allows Angular to inject other dependencies into it if needed.
Click to reveal answer
beginner
How can you provide a service at the root level in Angular?
You can provide a service at the root level by adding providedIn: 'root' in the @Injectable() decorator, making it a singleton available throughout the app.
Click to reveal answer
What does Angular use to decide which dependency to inject?
ARandom selection
BTokens like class types
CManual user input
DFile names
✗ Incorrect
Angular uses tokens, usually class types, to find and inject the correct dependency.
What decorator marks a class as injectable in Angular?
A@Component
B@Directive
C@Injectable
D@NgModule
✗ Incorrect
The @Injectable decorator tells Angular that the class can be injected as a dependency.
Where is a service provided if you use providedIn: 'root'?
AIn the root injector, available app-wide
BOnly in the component
CIn the module only
DNowhere, it disables the service
✗ Incorrect
providedIn: 'root' makes the service a singleton available throughout the entire app.
What is the Angular injector responsible for?
AManaging dependencies and their lifetimes
BHandling HTTP requests
CRendering UI
DStyling components
✗ Incorrect
The injector manages creating and providing dependencies to components and services.
How does Angular provide dependencies to a component?
ABy using global variables
BBy manually creating them inside the component
CBy importing them in the template
DBy injecting them through the constructor
✗ Incorrect
Angular injects dependencies by passing them as parameters in the component's constructor.
Explain how Angular's dependency injection system finds and provides a service to a component.
Think about how Angular uses the constructor and injector to supply dependencies.
You got /4 concepts.
Describe the purpose of the @Injectable() decorator in Angular dependency injection.
Consider what makes a class ready to be used as a service.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of dependency injection in Angular?
easy
A. To write all code inside a single file
B. To manually create instances of services inside components
C. To provide components with the services they need automatically
D. To prevent components from using any services
Solution
Step 1: Understand dependency injection concept
Dependency injection automatically provides required services to components without manual creation.
Step 2: Compare options with this concept
Only To provide components with the services they need automatically describes automatic provision of services, which is the core of dependency injection.
Final Answer:
To provide components with the services they need automatically -> Option C
Quick Check:
Dependency injection = automatic service provision [OK]
Hint: Dependency injection means Angular gives services automatically [OK]
Common Mistakes:
Thinking you must create service instances manually
Believing dependency injection restricts service use
Confusing dependency injection with code organization
2. Which decorator makes a class injectable in Angular?
easy
A. @Component
B. @Input
C. @NgModule
D. @Injectable
Solution
Step 1: Identify Angular decorators for injection
The decorator @Injectable marks a class as available for dependency injection.
Step 2: Eliminate other decorators
@Component is for components, @NgModule for modules, and @Input for input properties, none make a class injectable.
Final Answer:
@Injectable -> Option D
Quick Check:
@Injectable = makes class injectable [OK]
Hint: Use @Injectable to mark services for injection [OK]
Common Mistakes:
Confusing @Component with @Injectable
Using @NgModule to make services injectable
Forgetting to add @Injectable to services
3. Given this Angular service and component code, what will be logged?
import { Injectable } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class LoggerService {
log(message: string) {
console.log('Log:', message);
}
}
import { Component } from '@angular/core';
import { LoggerService } from './logger.service';
@Component({ selector: 'app-root', template: '' })
export class AppComponent {
constructor(private logger: LoggerService) {
this.logger.log('Hello');
}
}
medium
A. Log: Hello
B. Error: LoggerService not found
C. No output
D. Log: undefined
Solution
Step 1: Understand service injection in constructor
The LoggerService is injected into AppComponent via constructor and used to log a message.
Step 2: Analyze the log call
The log method prints 'Log:' plus the message 'Hello', so console shows 'Log: Hello'.
Final Answer:
Log: Hello -> Option A
Quick Check:
Injected service logs message correctly [OK]
Hint: Injected services work via constructor parameters [OK]
Common Mistakes:
Expecting error if service is providedIn root
Thinking log method won't run without template
Confusing console output with return value
4. What is wrong with this Angular service injection code?
import { Injectable } from '@angular/core';
@Injectable()
export class DataService {
getData() { return [1, 2, 3]; }
}
import { Component } from '@angular/core';
@Component({ selector: 'app-data', template: '' })
export class DataComponent {
constructor(private dataService: DataService) {}
}
medium
A. DataComponent constructor should not have parameters
B. DataService is not provided in any injector
C. DataService class is missing @Component decorator
D. getData method must be static
Solution
Step 1: Check service provider configuration
The service has @Injectable() but no providedIn or provider registration, so Angular doesn't know where to provide it.
Step 2: Understand injection failure cause
Without being provided, Angular throws an error when injecting DataService into DataComponent.
Final Answer:
DataService is not provided in any injector -> Option B
Quick Check:
Missing provider causes injection error [OK]
Hint: Always provide services via providedIn or providers array [OK]
Common Mistakes:
Assuming @Injectable alone provides the service
Adding @Component to services incorrectly
Thinking constructor parameters are optional for injection
5. You want to inject two different services of the same class but with different configurations in Angular. Which approach correctly achieves this?
hard
A. Use Angular injection tokens to provide different configurations and inject by token
B. Create two different classes extending the service and inject each separately
C. Use @Injectable({ providedIn: 'root' }) on the class and inject twice in constructor
D. Inject the service once and manually change its configuration before each use
Solution
Step 1: Understand multiple instances need
Injecting the same class twice with different configs requires distinguishing them in Angular's injector.
Step 2: Use injection tokens for different configs
Angular injection tokens allow providing multiple versions of the same class with different configurations and injecting them separately.
Step 3: Evaluate other options
Use @Injectable({ providedIn: 'root' }) on the class and inject twice in constructor injects the same instance twice, B duplicates classes unnecessarily, D is error-prone and not recommended.
Final Answer:
Use Angular injection tokens to provide different configurations and inject by token -> Option A