Retrieving service data from a component

To be able to use the getCurrentWeather function in the CurrentWeather component, you need to inject the service into the component:

  1. Inject the WeatherService into the constructor of the CurrentWeatherComponent class
  2. Remove the existing code that created the dummy data in the constructor:
src/app/current-weather/current-weather.component.tsconstructor(private weatherService: WeatherService) { }
  1. Call the getCurrentWeather function inside the ngOnInit function:
src/app/current-weather/current-weather.component.tsngOnInit() {  this.weatherService.getCurrentWeather('Bethesda', 'US')    .subscribe((data) => this.current = data)}

Fair warning, do not expect this code to be working just yet. You should see ...

Get Building Large-Scale Web Applications with Angular now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.