首页 > 解决方案 > 在 ngFor 中使用变量

问题描述

我有很多 ngFor 的表,我如何从 getList() 的项目中做 HTML 中的局部变量?

我需要用户#?

例子: <div *ngFor="let #item of getList()"></div>?以及如何在下一个 ngFor HTML 中使用局部变量?

标签: angularngfor

解决方案


我有表格示例:

<div class="card mb-4">
  <div class="card-body card-table">
    <div class="table-responsive">
      <table class="table table-striped table-bordered">
        <thead>
          <tr>
            <th class="align-middle">Przydział:</th>
            <th class="align-middle" *ngFor="let distinctStatus of groupByList(list, 'status')">{{distinctStatus.status}}</th>
          </tr>
        </thead>
        <tbody>
          <tr *ngFor="let distinctId of groupByList(list, 'id')">
            <td class="align-middle">{{distinctId.id}}</td>
            <td class="align-middle text-right" *ngFor="let distinctStatus of groupByList(list, 'status')">
              <span *ngFor="let value of groupByList(list, 'id', 'status')">
                <span *ngIf="value.id == distinctId.id && value.status == distinctStatus.status">{{value.number}}</span>
              </span>
            </td>
          </tr>
          <tr>
            <td class="align-middle font-weight-bold">Razem:</td>
            <td class="align-middle text-right font-weight-bold" *ngFor="let distinctStatus of groupByList(list, 'status')">
              <span *ngFor="let value of groupByList(list, 'status')">
                <span *ngIf="value.status == distinctStatus.status">{{value.number}}</span>
              </span>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</div>

我如何从 thead tr th 中获取 distinctStatus 变量,因为我需要在 body tr td 中将此变量与 ngIf 一起使用(将 value.property 与 distinct.property 进行比较。现在我有很多 ngFor,因为在 ngFor 范围之后我无法获得 distinctStatus 变量。


推荐阅读