In my map.service I have coded like this
loadAdminDataDiv (): Observable<any> {
return this.http.get("static/data/data.json")
.map(this.extractData);
}
private extractData(res) {
if (res.status < 200 || res.status >= 300) {
throw new Error('Bad response status: ' + res.status);
}
//console.log(res.json());
this.serviceData =(res.json()) ;
return this.serviceData || { };
}
Then injected inside a component as a provider and subscribed like this
getSubscribeData: any;
getAllvaluesFromFiles() {
this._mapService.loadAdminDataDiv().subscribe(data => {
this.getSubscribeData = data;
},
error => { console.log(error) },
() => {
console.log(this.getSubscribeData); // prints the data
}
);
}
ngAfterContentInit() {
this.getAllvaluesFromFiles();
console.log(this.getSubscribeData); // prints Undefined
}
Can anyone suggest me a work around with this problem ? My service is working but My subscribed data does not work outside of the subscribe method. I pointed out where I'm having problem. I need to use this this.getSubscribeData
throughout my component as a global variable. I dont want to use shared service, Is that possible without using another service. A working code will be helpful :)
And I have to load at least 7 more variable like this. Actually I need to use these variables to pass to d3 objects and into leaflet map and it has to be after some required interactions. Lets say, I have to load leafletmap with this this.getSubscribeData
so I will initiate the leaflet map inside the subscribe. but if I want to use this leaflet instance outside of the method what should I do?