我有一个使用* ngFor
的玩家列表
// Get all players.
getPlayers() {
this.httpService.getPlayers()
.subscribe(
player => {
this.players = player;
for (let i in this.players){
if(this.players[i].Main_Image_ID){
this.getPlayerIMG(this.players[i].Main_Image_ID);
}
}
},
error => this.errorMessage = <any>error);
}
<div ion-item *ngFor="let player of players">
<img *ngIf="player?.playerIMG" width="50" [src]="'data:image/png;base64,'+playerIMG" />
<img *ngIf="!player?.playerIMG" width="50" src="./assets/images/no-image.jpg" />
{{player.First_Name}} {{player.Last_Name}}
</div>
API调用返回的值返回具有多个播放器的对象数组。每个玩家都有一些信息和一个imageID。在我订阅电话并获得播放器后,将填充列表。然后我使用imageID调用另一个API来获取图像。我的目标是在我知道所有玩家及其imageID后,用他们的图像填充列表中的相应玩家。
// Get image for player.
getPlayerIMG(id) {
this.httpService.getPlayerIMG(id)
.subscribe(
playerIMG => {
this.player.playerIMG = playerIMG;
},
error => this.errorMessage = <any>error);
}
问题是如何将每个imageID调用的响应分配给列表(玩家)上的相应项目(玩家)。
谢谢!
答案 0 :(得分:1)
最好为播放器编写一个组件但你可以使用这个代码并将一个播放器传递给函数而不是Main_Image_ID
for (let i in this.players){
if(this.players[i].Main_Image_ID){
this.getPlayerIMG(this.players[i]);
}
}
// Get image for player.
getPlayerIMG(player) {
this.httpService.getPlayerIMG(player.Main_Image_ID)
.subscribe(
playerIMG => {
player.playerIMG = playerIMG;
},
error => this.errorMessage = <any>error);
}