在angular

时间:2019-04-03 16:50:19

标签: angular asynchronous rxjs observers

我正在尝试从get调用(getGameDetailsUsingGetCall())获取详细信息,并将其订阅到另一个函数中并在变量中分配值。当我在订阅块外使用该变量时,该变量未定义。

getGameDetailsUsingGetCall(gameId : String ){
           return this.http.get('https://test.com/games.json');
}

//gameDetail is a private variable 
getGameDetails(gameId: Number){
     this.dataStorageService.getGameDetailsUsingGetCall(gameId.toString())
  .subscribe(
    (response: Response)=>{
      console.log(response.json());
      this.gameDetail=<GameDetails>response.json();
      console.log('JoinGame details:'+this.gameDetail);
      //this.playerJoined= this.gameDetail.noOfPlayers;
    }
  )

console.log('JoinGame details:'+this.gameDetail); //undefined  
}

在订阅块外部访问时,this.gameDetail不确定。我知道有一些异步调用丢失了,但无法解决。

0 个答案:

没有答案