Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅当特定方法在angular 4中完成API请求时才执行代码_Angular_Api_Angular Material_Observable - Fatal编程技术网

仅当特定方法在angular 4中完成API请求时才执行代码

仅当特定方法在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

假设我有两种方法:方法一和方法二。方法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 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.
          }

      });
}