I am facing an issue with response from HTTP request in Angular 2. I am using ".subscriber" to get the response of the HTTP request.
Following is the code I have written :
NewJobDetailsComponent.ts :
ngOnInit() {
this.masterData = this.masterDataS.getMasterData(["designations"]);
//code breaking at below line
this.designations = this.masterData["designations"];
}
MasterDataS.ts :
private masterData = {};
private baseUrl = "some URL";
getMasterData(keys) {
let missingData = [];
for (let key of keys) {
this.baseAjaxService.doPost(this.baseUrl, missingData)
.subscribe(data => {
this.masterData[key] = data[key];
this.sessionStorage.setValue(key, data[key]);
});
}
return this.masterData;
}
In the above code, I fetch some data from server, after which I have to use the result to assign to this.designations.
Now to make the service commonly usable I have to use .subscriber for this request in MasterDataS.ts. Hence I am not able to write the .subscriber in NewJobDetailsComponent.ts
But as the code in NewJobDetailsComponent.ts depends upon the result of the HTTP request. How can I write the code in synchronous manner so that after getting the result of the HTTP only below statement will be executed??
this.designations = this.masterData["designations"];
This kind of issue I am getting at most of the times where the example only changes.