访问可观察的Angular内部的对象数组

时间:2018-06-27 00:32:09

标签: typescript components observable

我对angular非常陌生,我有一个Observable对象,在该对象中,我有一个称为玩家的对象数组。除了数组之外,我还可以显示对象内的所有其他属性。我究竟做错了什么?任何建议表示赞赏。

component.ts文件:

export class GameDetailComponent implements OnInit {

 game: Observable<any>;

  constructor(private gameService: GameService, private route: ActivatedRoute) { }

  getGame(key: string) {
    this.game = this.gameService.getGame(key);
  }

  ngOnInit() {
    //get key from url
    this.getGame(this.route.snapshot.params['id']);
  }

}

component.html

<div *ngIf="game | async as game">
  <h1>{{ game.name}}</h1>

  <div *ngFor="let player of game.players">
    <h3>{{player.name}}</h3>
  </div>
</div>

0 个答案:

没有答案