📖 Scenario: You are building a simple Angular app where one service needs to use another service to get data. This is common when services depend on each other to share logic or data.
🎯 Goal: Create two Angular services: DataService that provides a message, and LoggerService that uses DataService to log that message. Then inject LoggerService into a component to display the logged message.
📋 What You'll Learn
Create a service called DataService with a method getMessage() that returns the string 'Hello from DataService'.
Create a service called LoggerService that injects DataService in its constructor.
Add a method logMessage() in LoggerService that returns the message from DataService.getMessage().
Create a component called AppComponent that injects LoggerService and displays the logged message in its template.
💡 Why This Matters
🌍 Real World
Service-to-service injection is common in Angular apps to keep code modular and reusable. For example, a UserService might inject an ApiService to fetch user data.
💼 Career
Understanding dependency injection and service communication is essential for Angular developers to build maintainable and testable applications.
Progress0 / 4 steps
1
Create DataService with getMessage method
Create an Angular service called DataService with a method getMessage() that returns the string 'Hello from DataService'.
Angular
Hint
Use @Injectable({ providedIn: 'root' }) to make the service available app-wide.
2
Create LoggerService injecting DataService
Create an Angular service called LoggerService that injects DataService in its constructor and stores it in a private variable called dataService.
Angular
Hint
Inject DataService by adding it as a private parameter in the constructor.
3
Add logMessage method in LoggerService
Add a method called logMessage() inside LoggerService that returns the result of calling getMessage() on the injected dataService.
Angular
Hint
Use this.dataService.getMessage() inside logMessage().
4
Inject LoggerService in AppComponent and display message
Create an Angular component called AppComponent that injects LoggerService in its constructor. Add a public property message initialized by calling logMessage() on the injected loggerService. In the component template, display the message inside a <p> tag.
Angular
Hint
Inject LoggerService in the constructor and assign message by calling logMessage().
Practice
(1/5)
1. What is the main purpose of service-to-service injection in Angular?
easy
A. To allow one service to use another service's functionality easily
B. To create multiple instances of the same service
C. To directly manipulate the DOM inside a service
Hint: Injected service method output concatenated with string [OK]
Common Mistakes:
Assuming logger is undefined due to missing initialization
Mixing order of concatenated strings
Forgetting to call the log() method
4. What is wrong with this service-to-service injection code?
@Injectable({ providedIn: 'root' })
export class UserService {
constructor(private authService: AuthService) {}
}
// AuthService is not decorated with @Injectable
medium
A. UserService must be provided in a module, not root
B. AuthService must be decorated with @Injectable to be injected
C. Constructor should not have access modifier
D. UserService should not inject any service
Solution
Step 1: Check injection requirements
Angular requires any service to be decorated with @Injectable to be injectable into other services or components.
Step 2: Identify missing decorator
AuthService lacks @Injectable, so Angular cannot inject it into UserService.
Final Answer:
AuthService must be decorated with @Injectable to be injected -> Option B
Quick Check:
@Injectable missing on injected service = error [OK]
Hint: Always add @Injectable to services used for injection [OK]
Common Mistakes:
Thinking access modifier in constructor is wrong
Believing services can't be provided in root
Assuming services can't inject other services
5. You have two services: ApiService and CacheService. You want ApiService to use CacheService to store data before returning it. Which is the best way to implement this service-to-service injection pattern?
hard
A. Inject CacheService into ApiService constructor and call cache methods inside ApiService
B. Inject ApiService into CacheService and call API methods inside CacheService
C. Create a third service that duplicates both ApiService and CacheService methods
D. Use global variables to share data between ApiService and CacheService
Solution
Step 1: Understand service responsibilities
ApiService handles API calls, CacheService handles data storage. ApiService should use CacheService to store data.
Step 2: Apply service-to-service injection
Inject CacheService into ApiService's constructor, then call CacheService methods inside ApiService to store data before returning it.
Step 3: Avoid bad patterns
Injecting ApiService into CacheService or duplicating methods breaks separation of concerns. Using global variables is bad practice.
Final Answer:
Inject CacheService into ApiService constructor and call cache methods inside ApiService -> Option A
Quick Check:
Inject dependent service into main service for clean logic [OK]
Hint: Inject helper service into main service, not vice versa [OK]
Common Mistakes:
Injecting main service into helper service causing circular dependency
Duplicating logic instead of reusing services
Using global variables instead of injection
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.