下面是HTML代码:
<tr *ngFor="let user of users">
<td>
<h6 class="font-medium">{{user.id}}</h6>
</td>
<td>
<h6 class="font-medium">{{user.totalPrice}}</h6>
</td>
<td>
<button type="button" *ngFor="let user of users" (click)="downloadJson(user.id)"<i>icon</i></button></td>
字符串
下面是typescript方法:
downloadJson(userId: String) {
const user = this.users.find(u => u.id === userId);
if (user) {
const jsonString = JSON.stringify(user, null, 2);
const blob = new Blob([jsonString], { type: 'application/json' });
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = `user_data_${userId}.json`; // Include user ID in the file name
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} else {
console.error(`User with ID ${userId} not found`);
}
型
我已经尝试了不同的方法来删除它们,但它没有工作,我希望它只显示正确的id下载项目
1条答案
按热度按时间vlurs2pr1#
我发现问题出在
<button type="button" *ngFor="let user of users" (click)="downloadJson(user.id)"<i>icon</i></button></td>
问题出在*ngFor="let user of users"