I know this question has been asked a number of times but still none of the answer solves my problem. I have a service which fetch a URL from a json file.
@Injectable()
export class HostConfigService {
getHostUrl()
{
return this.http.get("File path").map((response : Response) =>
response.json()); // this returns a URL ex: http://localhost:8080
}
}
I want to use response from above URL to construct URL's in multiple files for rest calls from the angular application.
export class GetData{
constructor(private hostConfigService: HostConfigService, private _http:
Http) { }
getData(){
this.hostConfigService.getHostUrl().subscribe((res) => this.hostUrl =
res.hostUrl); //this.hostUrl should return undefined outside subscribe
block
this.restUrl = this.hostUrl + "/getData";
return this._http.get(this.restUrl).map((response :
Response) => response.json());
}
}
Response from getData function is also read from a subscribe. I already have used subscribe inside subscribe but that gives me error Property 'subscribe' does not exist on type 'void'. Can you please help me with a way to solve this problem?
You should use environment, but if you don't want to, the correct way of using your functions is like this
export class GetData {
constructor(private hostConfigService: HostConfigService, private _http: Http) { }
getData() {
this.hostConfigService.getHostUrl()
.flatMap(url => this._http.get(url).map((response : Response) => response.json()));
}
}
What happens is that you make the first call, and once the result is there, you make the second.