繁体   English   中英

我想访问位于component.ts中的更新变量,并将其分配给方法外的其他变量。

[英]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

服务

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

this.service.get_update(this.user).subscribe(update => this.update = update,
                                      error =>console.log(error));

我想访问位于component.ts中的更新变量,并将其分配给方法外的其他变量。

在某个其他方法中访问this.update将无济于事,因为该值已在回调中分配,并且其他一些方法将不等待其执行而导致空值。 您可以将函数回调作为get_update方法的输入,这样,在获取更新值时,可以将该值传递给相应的回调,或者可以在回调内部直接调用另一个方法,将更新值作为输入参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM