仅当特定方法在angular 4中完成API请求时才执行代码
假设我有两种方法:方法一和方法二。方法1在方法2中有一个调用,如下所示:仅当特定方法在angular 4中完成API请求时才执行代码,angular,api,angular-material,observable,Angular,Api,Angular Material,Observable,假设我有两种方法:方法一和方法二。方法1在方法2中有一个调用,如下所示: variable; methodOne(){ this.variable; // A API call which sets the value of "variable" }; methodTwo(){ this.methodOne(); if(this.variable!= null){ // using data in variable for
variable;
methodOne(){
this.variable;
// A API call which sets the value of "variable"
};
methodTwo(){
this.methodOne();
if(this.variable!= null){
// using data in variable for other operations
}
}
现在的问题是,当调用methodTwo
时,它在内部调用methodOne
,该函数通过API返回的数据设置“variable”的值。但是methodOne
调用methodTwo
后的代码在设置“变量”数据之前执行,并且变量的值始终为空
methodOne(){
// return this.http.get('whatever');
};
methodTwo(){
this.methodOne().subscribe((whateverResponse)=>{
// set this.variable here with whateverResponse
if(this.variable !=null){
// do whatever here.
}
});
}