Populating a table using two different * ngFor

The following is an array of arrays of JSON objects:

{
  "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
    }
  ]
}

I want to display these attributes, namely. the value, count, and tagName (which is executed using tagId) in the table. For the first two attributes, I use ngFor. But I also want to print the tagName, which I get with tagId and save it in the tagNames array. The following is the code for my component:

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)
      }
    )
  }

And this is how I try to print them in the user interface:

<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>

But I get [object of object] under the tag name column. Can someone help me solve this problem?

I tried using an ng container as above, but the result looks like this: tag-name-issue

. 3 "Subtag 3", "Zeit1", "Tag 1" 1,2,3 .

.

+4
2

, * 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: string [] = [];

+2

*ngFor . <ng-container>,

  <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>
+3

Source: https://habr.com/ru/post/1672064/


All Articles