Commit 2b300ce5 authored by DangSauQuaKhu's avatar DangSauQuaKhu

add array city

parent a78baa58
...@@ -7,7 +7,7 @@ const routes: Routes = [ ...@@ -7,7 +7,7 @@ const routes: Routes = [
{ {
path: ':slug', path: ':slug',
component: AnothercityComponent , component: TodayComponent ,
} }
]; ];
......
...@@ -2,5 +2,5 @@ ...@@ -2,5 +2,5 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head> </head>
<div class="container"> <div class="container">
<app-today></app-today> <app-today [cities]="cities"></app-today>
</div> </div>
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute } from '@angular/router';
import { ShareReplayConfig } from 'rxjs';
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
...@@ -8,5 +9,6 @@ import { ActivatedRoute } from '@angular/router'; ...@@ -8,5 +9,6 @@ import { ActivatedRoute } from '@angular/router';
}) })
export class AppComponent { export class AppComponent {
title = 'weatherAPI'; title = 'weatherAPI';
cities: any[] = new Array();
constructor(private _route: ActivatedRoute){} constructor(private _route: ActivatedRoute){}
} }
...@@ -2,33 +2,44 @@ ...@@ -2,33 +2,44 @@
<div class="col-md-6 offset-md-3 "> <div class="col-md-6 offset-md-3 ">
<div class="col-sm-12 text-center"> <div class="col-sm-12 text-center">
<div class="input-group mt-5"> <div class="input-group mt-5">
<input type="text" class="form-control" #userInput placeholder="city name" (keyup.enter)="getCity(userInput.value)"> <input type="text" class="form-control" #userInput placeholder="city name"
(keyup.enter)="getCity(userInput.value)">
<div class="input-group-append"> <div class="input-group-append">
<button class="input-group-text btn btn-outline-primary" (click)="getCity(userInput.value)" >Search</button> <button class="input-group-text btn btn-outline-primary"
(click)="getCity(userInput.value)">Search</button>
</div> </div>
</div> </div>
<h2>Weather in {{weather.name}}, {{weather.sys.country}}</h2>
</div> </div>
</div> </div>
<div class="col-sm-12 text-center"> <div *ngFor="let city of cities">
<span class="temp">{{weather.main.temp|number:'1.0-0'}}&#176;F</span> <h2 class="text-center">Weather in {{city.name}}, {{city.sys.country}}</h2>
<p class="text-uppercase font-italic font-weight-bold" >{{weather.weather[0].description}}</p> <div class="col-sm-12 text-center">
<span class="temp">{{city.main.temp|number:'1.0-0'}}&#176;F</span>
<p class="text-uppercase font-italic font-weight-bold">{{city.weather[0].description}}</p>
</div>
<table class="table table-light table-striped table-bordered table-sm text-center"
style="width: 70%; margin-left: 13%;">
<tbody>
<tr>
<td>Humidity</td>
<td>{{city.main.humidity}}%</td>
</tr>
<tr>
<td>Pressure</td>
<td>{{city.main.pressure}} hPa</td>
</tr>
<tr>
<td>Sunrise</td>
<td>{{city.sys.sunrise*1000 | date: 'H:mm'}}</td>
</tr>
<tr>
<td>Sunset</td>
<td>{{city.sys.sunset*1000|date: 'H:mm'}}</td>
</tr>
</tbody>
</table>
</div> </div>
<table class="table table-light table-striped table-bordered table-sm text-center" style="width: 70%; margin-left: 13%;">
<tbody>
<tr>
<td>Humidity</td><td>{{weather.main.humidity}}%</td>
</tr>
<tr>
<td>Pressure</td><td>{{weather.main.pressure}} hPa</td>
</tr>
<tr>
<td>Sunrise</td><td>{{weather.sys.sunrise*1000 | date: 'H:mm'}}</td>
</tr>
<tr>
<td>Sunset</td><td>{{weather.sys.sunset*1000|date: 'H:mm'}}</td>
</tr>
</tbody>
</table>
</div> </div>
\ No newline at end of file
import { Component, OnInit } from '@angular/core'; import { Component, Input, OnInit } from '@angular/core';
import { WeatherService } from '../weather.service'; import { WeatherService } from '../weather.service';
@Component({ @Component({
...@@ -7,6 +7,7 @@ import { WeatherService } from '../weather.service'; ...@@ -7,6 +7,7 @@ import { WeatherService } from '../weather.service';
styleUrls: ['./today.component.css'] styleUrls: ['./today.component.css']
}) })
export class TodayComponent implements OnInit { export class TodayComponent implements OnInit {
@Input() cities: any[];
lat; lat;
lon; lon;
weather; weather;
...@@ -23,6 +24,8 @@ export class TodayComponent implements OnInit { ...@@ -23,6 +24,8 @@ export class TodayComponent implements OnInit {
this.weatherService.getWeatherDataByCoords(this.lat,this.lon).subscribe( this.weatherService.getWeatherDataByCoords(this.lat,this.lon).subscribe(
data=>{ data=>{
this.weather= data; this.weather= data;
if(this.cities.findIndex(element=>element.name === this.weather.name)==-1)
this.cities.push(data);
console.log(this.weather); console.log(this.weather);
} }
) )
...@@ -32,10 +35,13 @@ export class TodayComponent implements OnInit { ...@@ -32,10 +35,13 @@ export class TodayComponent implements OnInit {
} }
getCity(city) getCity(city)
{ {
this.weatherService.getWeatherDataByCityName(city).subscribe( this.weatherService.getWeatherDataByCityName(city).subscribe(
data=>{ data=>{
this.weather= data; this.weather= data;
console.log(this.weather); console.log(this.weather);
if(this.cities.findIndex(element=>element.name === this.weather.name)==-1)
this.cities.push(data);
} }
) )
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment