简体   繁体   中英

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

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

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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