【问题标题】:Populating table using 2 different *ngFor使用 2 个不同的 *ngFor 填充表
【发布时间】:2017-08-01 22:44:44
【问题描述】:

下面是我的 JSON 对象数组:

{
  "tagFrequency": [
    {
      "value": "aenean",
      "count": 1,
      "tagId": 251
    },
    {
      "value": "At",
      "count": 1,
      "tagId": 249
    },
    {
      "value": "faucibus",
      "count": 1,
      "tagId": 251
    },
    {
      "value": "ipsum",
      "count": 1,
      "tagId": 251
    },
    {
      "value": "lobortis",
      "count": 1,
      "tagId": 194
    },
    {
      "value": "molestie",
      "count": 1,
      "tagId": 251
    },
    {
      "value": "unde tempor, interdum ut orci metus vel morbi lorem. Et arcu sed wisi urna sit egestas fringilla, at erat. Dolor nunc.",
      "count": 1,
      "tagId": 199
    },
    {
      "value": "Vestibulum",
      "count": 1,
      "tagId": 251
    }
  ]
}

我想显示这些属性,即。表中的值、计数和 tagName(使用 tagId 获取)。对于前两个属性,我使用的是 ngFor。但是,我还想打印我使用 tagId 获取的 tagName 并将其存储在 tagNames 数组中。以下是我的组件代码:

frequencies: any;
tagNames: string[] = [];

ngOnInit() {
    if (this.route.snapshot.url[0].path === 'tag-frequency') {
      let topicId = +this.route.snapshot.params['id'];

      this.tagService.getTagFrequency(topicId)
        .then(
            (response: any) => {
          this.frequencies = response.json().tagFrequency
          for(let tagFrequency of this.frequencies) {
            this.getTagName(tagFrequency.tagId)
          }
        }
        )
        .catch(
            (error: any) => console.error(error)
        )
    }
}

  getTagName(tagId: number): string {
    return this.tagService.getTag(tagId)
    .then(
        (response: any) => {
          this.tagNames.push(response.name)
        }
    )
    .catch(
      (error: any) => {
        console.error(error)
      }
    )
  }

这就是我尝试在 UI 上打印它们的方式:

<table>
  <thead>
    <tr>
      <th>{{ 'word' }}</th>
      <th>{{ 'tag-name' }}</th>
      <th>{{ 'frequency' }}</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
      <ng-container *ngFor="let name of tagNames">
        <tr *ngFor="let frequency of frequencies; let i=index">
          <td>{{ frequency.value }}</td>
          <td>{{ name }}</td>
          <td>{{ frequency.count }}</td>
        </tr>
      </ng-container>
  </tbody>
</table>

但我在列标记名下得到 [object object]。有人可以帮我解决这个问题吗?

我尝试像上面那样使用 ng-container,但结果在 UI 上看起来像这样:

这是错误的。对于第 1、2、3 行,我只需要前 3 行标签名称分别为“Subtag 3”、“Zeit1”、“Tag 1”。

提前致谢。

【问题讨论】:

  • 用json数据更新帖子,方便其他人

标签: angular html-table ngfor


【解决方案1】:

您可以使用索引变量,遍历 *ngFor 中的频率数组并将其索引用于 tagNames

<tr *ngFor="let frequency of frequencies; let i=index">
      <td>{{ frequency.value }}</td>
      <td>{{ tagNames[i] }}</td>
      <td>{{ frequency.count }}</td>
  </tr>

确保您已初始化 tagNames:

标签名称:字符串[] = [];

【讨论】:

    【解决方案2】:

    您不能在单个元素上使用双 *ngFor。您可以使用辅助元素&lt;ng-container&gt; 喜欢

      <tr *ngFor="let frequency of frequencies; let i=index">
        <ng-container *ngFor="let name of tagNames">
          <td>{{ frequency.value }}</td>
          <td>{{ name }}</td>
          <td>{{ frequency.count }}</td>
        </ng-container>
      </tr>
    

    【讨论】:

    • 对不起。它不适用于多个元素。例如。如果我总共有 8 行,那么这 3 个属性在单行上填充 8 次。我只是想将其打印为 8 x 3 矩阵。
    • 我猜你只需要删除&lt;ng-container&gt;元素,或者将它移到&lt;tr&gt;之外
    • 删除 是什么意思?如果我将它移到 之外,将会出现 64 行,这意味着它重复了 8 次。我只想要 8 行。不知何故,我不想嵌套 for 循环。
    • 我不知道你想要什么。
    • 我将提供我想要的和当前呈现的内容的快照
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签