我正在开发自己的项目,即一个游戏库,我有一个单独的文件,其中包含我的硬编码数据,其中每个项目都有一个ID和一个名称。我遇到的问题是我的component.ts文件看不到id的值,即使我的get请求工作得很好,并在表中显示了id和名称。我遵循了一个教程,但对其进行了调整,以使其适用于我,但是当我无法就自己的具体问题寻求帮助时,这会导致问题。
我曾尝试过更改变量名,以防万一我搞砸了,但是没有这样做,所以我不确定老实说。
<table>
<tr>
<th></th>
<th>Id</th>
<th>Name</th>
</tr>
<tr *ngFor="let game of games">
<button (click)="updateGame($game)" class="btn btn-default btn-sm">Update</button>
<td>{{game.id}}</td>
<td>{{game.name}}</td>
</tr>
</table>
export class UpdateGameBodyComponent {
public games: any = [];
constructor(private httpClient: HttpClient) {
httpClient.get('http://localhost:3000/api/games').subscribe(response => {
this.games = response;
});
}
updateGame(game) {
this.httpClient.put('http://localhost:3000/api/games' + '/' + game.id, JSON.stringify({
isRead: true
}))
.subscribe(response => {
console.log(response);
})
}
};
当我单击更新按钮时,控制台显示错误消息,提示它无法获取Id的值,我想解决该问题,以便我可以继续使放置请求工作。
答案 0 :(得分:1)
在HTML中将“ $ game”更改为“ game”,因为* ngFor返回的值不是DOM事件
<button (click)="updateGame(game)" class="btn btn-default btn-sm">Update</button>