From e05b9df49ed80d56befe079b62854e1c07f83c3d Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Thu, 9 Jun 2022 21:49:00 +0200 Subject: [PATCH] fix: json load with observable --- src/app/app.component.ts | 6 +++++- src/app/json-file.service.ts | 19 +++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 13ef1bd..386cba1 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -27,7 +27,11 @@ export class AppComponent public fileEvent(event: any): void { - this.foodcard = this.jsonFileService.importJson(event); + let importobserv = this.jsonFileService.importJson(event); + importobserv.subscribe((next) => { + this.foodcard = next; + console.log(next); + }); } public addTitle(): void diff --git a/src/app/json-file.service.ts b/src/app/json-file.service.ts index a58a660..f6f3d65 100644 --- a/src/app/json-file.service.ts +++ b/src/app/json-file.service.ts @@ -1,5 +1,6 @@ import { Injectable } from '@angular/core'; import { DomSanitizer, SafeUrl } from '@angular/platform-browser'; +import { Observable } from 'rxjs'; import { IFoodCard } from './ifood-card'; @Injectable({ @@ -17,16 +18,18 @@ export class JsonFileService "data:text/json;charset=UTF-8," + json); } - public importJson(event: any): IFoodCard + public importJson(event: any): Observable { - let foodcard: IFoodCard = { Titles: [] }; + let observ = new Observable((observ) => { + let foodcard: IFoodCard = { Titles: [] }; - const reader = new FileReader(); - reader.onload = (e: any) => { - foodcard = JSON.parse(e.target.result); - } - reader.readAsText(event.target.files[0]); + const reader = new FileReader(); + reader.onload = (e: any) => { + observ.next(JSON.parse(e.target.result)); + } + reader.readAsText(event.target.files[0]); + }); - return foodcard; + return observ; } } -- 2.39.5