1. we need a service get the weather

 1 import {Injectable} from '@angular/core';
 2 import {HttpClient, HttpResponse} from '@angular/common/http';
 3 import {Observable, of} from 'rxjs';
 4 import {LocalStorageService} from 'angular-web-storage';
 5 
 6 /**
 7  *  data from tianqiapi
 8  */
 9 @Injectable({
10   providedIn: 'root'
11 })
12 export class WeatherService {
13   private url = 'https://www.tianqiapi.com/api/?version=v6';
14 
15   constructor(private httpClient: HttpClient,
16               public localStorageService: LocalStorageService) {
17   }
18 
19 
20   getWeather(): Observable<Weather> {
21     let weather: Weather;
22     weather = this.localStorageService.get(Weather.identifier);
23     if (weather !== undefined && weather !== null) {
24       return of(weather);
25     }
26     return new Observable<Weather>(subscriber => {
27       this.loadWeather().subscribe(res => {
28         if (res.ok) {
29           subscriber.next(res.body);
30           this.localStorageService.set(Weather.identifier, res.body);
31           subscriber.complete();
32         }
33       }, error => {
34         subscriber.error(error);
35         subscriber.complete();
36       });
37     });
38   }
39 
40 
41   private loadWeather(): Observable<HttpResponse<Weather>> {
42     return this.httpClient.get<Weather>(this.url, {
43       observe: 'response'
44     });
45   }
46 
47 }
48 
49 export class Weather {
50   static identifier = 'weather$';
51   'cityid': string;
52   'date': string;
53   'week': string;
54   'update_time': string;
55   'city': string;
56   'cityEn': string;
57   'country': string;
58   'countryEn': string;
59   'wea': string;
60   'wea_img': string;
61   'tem': string;
62   'win': string;
63   'win_speed': string;
64   'win_meter': string;
65   'humidity': string;
66   'visibility': string;
67   'pressure': string;
68   'air': string;
69   'air_pm25': number;
70   'air_level': string;
71   'air_tips': string;
72   'alarm': {
73     'alarm_type': string;
74     'alarm_level': string;
75     'alarm_content': string;
76   };
77 }
WeatherService.ts

2.  and a pipe to transform data to Html Element

 1 /**
 2  * ng2 pipe to transform  weather to ex:
 3  * <b><sup>雨</sup><span>广州</span><br><span>27ºC</span></b>
 4  */
 5 @Pipe({name: 'weather'})
 6 export class WeatherPipe implements PipeTransform {
 7   /**
 8    *
 9    * @param weather see Weather
10    * @param e   a element reference , or null to create new  element contains data
11    * @param type    replace/appendChild/createElement at where invoke this method
12    */
13   transform(weather: Weather, e?: Element, type?: Tpye): Element {
14     const div: HTMLElement = document.createElement('div');
15     const b: HTMLElement = document.createElement('b');
16     const citySpan: HTMLElement = document.createElement('span');
17     const temSpan: HTMLElement = document.createElement('span');
18     const sup: HTMLElement = document.createElement('sup');
19 
20     sup.innerText = weather.wea;
21     citySpan.innerText = weather.city;
22     temSpan.innerText = `${weather.tem}ºC`;
23     b.appendChild(sup);
24     b.appendChild(citySpan);
25     b.appendChild(document.createElement('br'));
26     b.appendChild(temSpan);
27     if (!e) {
28       type = Tpye.create;
29     }
30     switch (type) {
31       case undefined || null || Tpye.child:
32         e.appendChild(b);
33         break;
34       case Tpye.replace:
35         e.replaceWith(b);
36         break;
37       case Tpye.create:
38         return div.appendChild(b);
39 
40     }
41 
42   }
43 }
44 
45 export enum Tpye {
46   replace,
47   child,
48   create
49 }
WeatherPipe.ts

3. then apply it to html 

 1 ....
 2  <div *ngIf="weather" #weatherDiv class="weather-float-right">
 3               {{weather|weather:weatherDiv:type}}
 4             </div>
 5 
 6 .....
 7 
 8 <style>
 9 .weather-float-right {
10   float: right;
11   font-size: 1.2em;
12 }
13 .weather-float-right sup{
14   font-size: 0.8em;
15 }
16 </style>
app.component.html

4. don't forget declaring it in module 

 

1 @NgModule({
2   declarations: [
3     WeatherPipe
4 ],
5 ...
app.module.ts

 

 

 

 check this at gist

posted on 2019-06-17 14:20  四维胖次  阅读(102)  评论(0编辑  收藏  举报