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

dwbf0jvd  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(144)

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

vlurs2pr

vlurs2pr1#

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

相关问题