简体   繁体   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

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.

相关问题 在角度,我如何从component.ts访问类方法? - In angular, how do I access a class method from component.ts? 如何在 Angular component.ts 中的 function 之外声明 HTMLElement 变量? - How to declare HTMLElement variable outside of a function in Angular component.ts? 为什么 component.ts 不能分配给接口的变量? - Why the component.ts cannot assign to the interface's variable? Angular Material MatTableModule - 如何从模板中的 mat-table 外部访问 component.ts 文件中返回的数据? - Angular Material MatTableModule - How can I access the returned data from component.ts file from outside the mat-table in the template? angular 2 4 5-如何在component.ts中定义对象变量 - angular 2 4 5 - how to define object variable in component.ts 如何将输入 keyup 值绑定到 component.ts 中的变量? - How to bind input keyup value to a variable in component.ts? 在 component.ts Angular 中使用可变颜色 - Use variable color in component.ts Angular Angular - 当它们在 component.ts 文件中启动时,如何从指令内部检测表单事件? - Angular - How do I detect form events from inside a directive when they are initiated in component.ts file? Angular2:我如何从component.ts导航到子项 - Angular2: how do i navigate to children from component.ts 如何从.component.ts文件中* ngFor生成的输入中获取值? - How do I get the values from the inputs that *ngFor generate in my .component.ts file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM