我需要通过http.get
获取变量的值(必须是异步的):
@Injectable()
export class interaction{
private host: string="";
private port: string = "";
constructor(private http: Http) {
this.http.get("/interaction.json").subscribe((data: Response) => {
this.host = data.json().host;
this.port = data.json().port;
});
}
由于数据是异步到来的,我无法在下面的方法中访问它们:
interact(data:Request): Promise<Response>{
return this.http.post("http://" + this.host + ":" + this.port, data)
.toPromise()
.then(data => data.json() as Response)
.catch(this.handleError);
}
因此,当我从另一个服务调用interact()
时,host
和port
是未定义的。是否可以等待此变量被设置,然后执行post
请求?我不想在interact()
中进行get
调用,因为可以有很多函数使用这些变量。
如果有任何建议,我将不胜感激。
https://stackoverflow.com/questions/47551148
复制