Implementing Dependency Injection in Angular: A Weather Service Challenge
Dependency injection (DI) is a core concept in Angular, promoting modularity, testability, and reusability. This challenge asks you to implement a simple weather service and inject it into a component, demonstrating a fundamental understanding of Angular's DI system. Successfully completing this challenge will solidify your grasp of how to provide and consume dependencies within an Angular application.
Problem Description
You need to create an Angular application with a WeatherService and a WeatherComponent. The WeatherService will simulate fetching weather data (for simplicity, it will return a hardcoded value). The WeatherComponent will display the weather data provided by the WeatherService. The key requirement is to use Angular's dependency injection to provide the WeatherService to the WeatherComponent.
What needs to be achieved:
- Create a
WeatherServiceclass with a methodgetWeather()that returns a string representing the weather (e.g., "Sunny", "Cloudy", "Rainy"). - Create a
WeatherComponentthat injects theWeatherService. - In the
WeatherComponent, callgetWeather()from the injectedWeatherServiceand display the result in the component's template. - Register the
WeatherServiceas a provider in the application module.
Key Requirements:
- The
WeatherComponentshould not directly instantiate theWeatherService. It should receive it through dependency injection. - The
WeatherServiceshould be registered as a provider in the Angular module. - The application should display the weather data fetched from the service.
Expected Behavior:
When the WeatherComponent loads, it should display the weather data returned by the WeatherService. The weather data should be a string like "Sunny", "Cloudy", or "Rainy".
Edge Cases to Consider:
- While this example doesn't require error handling, consider how you might handle errors in a real-world scenario where the service might fail to fetch data. (This is not required for this challenge, but a good thought exercise).
Examples
Example 1:
Input: Application loads, WeatherComponent is rendered.
Output: The WeatherComponent displays "Sunny".
Explanation: The WeatherService returns "Sunny", and the WeatherComponent displays this value.
Example 2:
Input: Application loads, WeatherComponent is rendered.
Output: The WeatherComponent displays "Rainy".
Explanation: The WeatherService returns "Rainy", and the WeatherComponent displays this value.
Constraints
- The solution must be written in TypeScript.
- The
getWeather()method inWeatherServiceshould return a string. - The application should be a functional Angular application.
- No external libraries beyond Angular itself are allowed.
- The solution should be relatively concise and easy to understand.
Notes
- Remember to import the necessary modules and decorators (e.g.,
@Component,@Injectable). - Use the
@Injectable()decorator to mark theWeatherServiceas a provider. - Use the constructor of the
WeatherComponentto inject theWeatherService. - Consider using template interpolation (
{{ weatherData }}) in the component's template to display the data. - Focus on the core concept of dependency injection – how the component receives the service without creating it directly.