I would like to get data with a subject from http. I have a service (storage.Service) that "call" the http connection with the subject and copy the array to another service (myinfo.Service) that will pass the data to the page.ts
I arrive to get the array and pass it to myinfo.service, but i don't arrive to copy it, because after i call the metode to copy the info.service array to the page.ts th array is empty.
Thanks so much for your help.
info.service code:
private myinfoData: MyinfoModel[] = [];
myinfoSubject = new Subject<MyinfoModel[]>();
constructor() { }
setInfo(myinfoData: MyinfoModel[]) {
this.myinfoData = myinfoData;
this.myinfoSubject.next(this.myinfoData.slice());
}
getInfo() {
return this.myinfoData.slice();
}
storage.service code:
private _url: any = '../assets/DB-Test/DB_MyInfo.json';
constructor(private http: HttpClient, private myinfoService: MyinfoService) { }
getMyInfo() {
this.http.get<any[]>(this._url).subscribe(
(response: MyinfoModel[]) => {
const infoData: MyinfoModel[] = response['DB_Portfolio']['TB_MyInfo'];
this.myinfoService.setInfo(infoData);
console.log(response);
console.log(infoData);
},
(error) => {
console.log('Error: ' + error);
}
);
}
Page.ts code:
infoDataBis: MyinfoModel[];
constructor(private dbStorage: DbStorageService, private myinfoService: MyinfoService) {
this.dbStorage.getMyInfo();
}
ngOnInit() {
this.infoDataBis = this.myinfoService.getInfo();
console.log(this.infoDataBis);
on the console.log I've wrote in the storage.service
console.log(response);
console.log(infoData);
I see the arrays
on the console.log I've wrote in the page.ts
console.log(this.infoDataBis);
the array is empty
When i debug i see the array in the info.service getting some that during the "call" of the storage.service then it get empty. So when i call the method getInfo() i copy an empty array.
With this logique, i thought i could copy the data to a service that i can re-use in any page.ts i want later. But i don't arrived to do it :(
Thanks for your help!!