Skip to content

Commit

Permalink
Handle changing params to city component
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyoconnor committed May 23, 2018
1 parent 1bda146 commit d2e93bf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
11 changes: 11 additions & 0 deletions src/app/app.component.html
@@ -1 +1,12 @@
<router-outlet></router-outlet>

<p>Some test cities:</p>
<div>
<a routerLink="/city/vancouver">Vancouver</a>
</div>
<div>
<a routerLink="/city/limerick">Limerick</a>
</div>
<div>
<a routerLink="/city/fakecity">Fake</a>
</div>
30 changes: 19 additions & 11 deletions src/app/city/city.component.ts
@@ -1,6 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, OnChanges, SimpleChanges } from '@angular/core';
import { WeatherService } from '../weather.service';
import { ActivatedRoute } from '@angular/router';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
selector: 'app-city',
Expand All @@ -19,16 +19,24 @@ export class CityComponent implements OnInit {
}

ngOnInit() {
this.city = this.route.snapshot.params['city'];

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;
});
this.route.paramMap.subscribe(route => {
this.city = route.get('city');
this.reset();
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;
});
});
}

reset() {
this.failedToLoad = false;
this.weather = '?';
this.temp = 0;
}
}

0 comments on commit d2e93bf

Please sign in to comment.