[英]I want to access the update variable which is in component.ts and i want to assign it to other variable outside the method.How to do it
service.ts 服务
get_update(id:any): Observable<any[]>{
let headers = new Headers({ 'Content-Type': 'application/json'});
let options = new RequestOptions({ headers: headers });
return this.http.get('http://localhost:8000/vendor/'+id,options)
.map((res: Response) => res.json())
.catch(error => Observable.throw(error.statusText));
}
component.ts component.ts
this.service.get_update(this.user).subscribe(update => this.update = update,
error =>console.log(error));
I want to access the update variable which is in component.ts and i want to assign it to other variable outside the method.How to do it 我想访问位于component.ts中的更新变量,并将其分配给方法外的其他变量。
Accessing this.update in some other method will not help as the value is getting assigned inside a callback and some other method will not wait for its execution resulting in null value. 在某个其他方法中访问this.update将无济于事,因为该值已在回调中分配,并且其他一些方法将不等待其执行而导致空值。 Either you can have a function callback as an input for get_update method, such that when you get update value you can pass that value to the respective callback or you can directly call the other method inside your callback passing the update value as input parameter.
您可以将函数回调作为get_update方法的输入,这样,在获取更新值时,可以将该值传递给相应的回调,或者可以在回调内部直接调用另一个方法,将更新值作为输入参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.