如果有 2 个用户,我如何删除此 angular 代码中额外的 3 个下载图标

How do i remove the extra 2 download icons in this angular code if there are 3 users

提问人:Gledi 提问时间:11/10/2023 更新时间:11/10/2023 访问量:28

问:

所以这里是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>

这是打字稿方法:

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 的下载项目

javascript html json angular

评论

2赞 VLAZ 11/10/2023
如果你想要一个下载按钮,那为什么要这样做?您已经有上述内容。您为每个用户创建一行,然后在该行中为每个用户创建一个按钮。不要。制作一个按钮。<button type="button" *ngFor="let user of users"ngFor

答:

0赞 Gledi 11/10/2023 #1

我发现问题出在问题中<button type="button" *ngFor="let user of users" (click)="downloadJson(user.id)"<i>icon</i></button></td>*ngFor="let user of users"