Hone logo
Hone
Problems

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:

  1. Create a WeatherService class with a method getWeather() that returns a string representing the weather (e.g., "Sunny", "Cloudy", "Rainy").
  2. Create a WeatherComponent that injects the WeatherService.
  3. In the WeatherComponent, call getWeather() from the injected WeatherService and display the result in the component's template.
  4. Register the WeatherService as a provider in the application module.

Key Requirements:

  • The WeatherComponent should not directly instantiate the WeatherService. It should receive it through dependency injection.
  • The WeatherService should 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 in WeatherService should 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 the WeatherService as a provider.
  • Use the constructor of the WeatherComponent to inject the WeatherService.
  • 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.
Loading editor...
typescript