24 lines
544 B
TypeScript
24 lines
544 B
TypeScript
import { HttpClient } from '@angular/common/http';
|
|
import { Pipe, PipeTransform } from '@angular/core';
|
|
|
|
@Pipe({
|
|
name: 'fetch',
|
|
pure: false
|
|
})
|
|
export class FetchJsonPipe implements PipeTransform {
|
|
private cachedData: any = null;
|
|
private cachedUrl = '';
|
|
|
|
constructor(private http: HttpClient) { }
|
|
|
|
transform(url: string): any {
|
|
if (url !== this.cachedUrl) {
|
|
this.cachedData = null;
|
|
this.cachedUrl = url;
|
|
this.http.get(url).subscribe(result => this.cachedData = result);
|
|
}
|
|
|
|
return this.cachedData;
|
|
}
|
|
}
|