My http-data.service is set up to accept json for output in the component template. Initially, the console displays that the first few calls return undefined, while subsequent calls start returning json. However, upon checking the component, it is evident that the method responsible for outputting the data to the component is only called once. As a result, since the data has not arrived yet, it shows undefined and does not update even after the json has arrived. Can someone please help explain why this is happening? Thank you.
Code snippet from my http-data.service
:
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import {Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class HttpService{
constructor(private http: Http) {}
getDataOrganizations(): Observable<any[]>{
return this.http.get('http://localhost:3010/data')
.map((resp:Response)=>{
let dataOrganizations = resp.json().organization;
return dataOrganizations;
});
}
// Other similar methods follow here...
}
Snippet from my data-all.service
import { Injectable, EventEmitter } from '@angular/core';
// Other imports...
@Injectable()
export class ModuleDataService {
// Constructor and other properties/functions...
private currentPopupView: EventEmitter<any> = new EventEmitter<any>();
// More properties and functions...
}
Snippet from my left-block.component
import { Component, OnInit} from '@angular/core';
// Other imports...
@Component({
// Component setup details...
})
export class ModuleLeft implements OnInit{
modules: ModuleModel[];
constructor(private modulesAll: ModuleDataService){}
ngOnInit(){
this.modules = this.modulesAll.getDataModules();
console.log("view");
console.log(this.modulesAll.getDataModules());
}
onToggle(module: any){
this.modulesAll.toggleModules(module);
}
}
In my component, the this.modulesAll.getDataModules()
method seems to be executed only once without updating (console logs => undefined). If anyone has any insights or thoughts on this issue, I would appreciate some help. Thanks.