Súbor src/app/app.component.html:
<router-outlet></router-outlet>
<app-city></app-city>
Súbor src/app/app.component.ts:
import { Component, OnInit } from '@angular/core';
import { WeatherService } from './weather.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.less']
})
export class AppComponent {
}
Súbor src/app/app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { CityComponent } from './city/city.component';
@NgModule({
declarations: [
AppComponent,
CityComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Súbor src/app/city/city.component.html:
<div>{{city}}</div>
<div>{{weather}}</div>
<div>{{temp}}</div>
<div *ngIf="failedToLoad">Weather is not available. Try looking out the window.</div>
Súbor src/app/city/city.component.ts:
import { Component, OnInit } from '@angular/core';
import { WeatherService } from '../weather.service';
@Component({
selector: 'app-city',
templateUrl: './city.component.html',
styleUrls: ['./city.component.less']
})
export class CityComponent implements OnInit {
city = 'Vancouver';
weather = '?';
temp = 0;
failedToLoad: boolean;
constructor(public weatherService: WeatherService) {
}
ngOnInit() {
this.weatherService.getCurrentWeather(this.city).subscribe(x => {
this.weather = x.weather.description;
this.temp = x.temp;
},
error => {
console.log('error occured', error);
this.failedToLoad = true;
});
}
}