Ниже мой массив объектов 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
}
]
}
Я хочу отобразить эти атрибуты, а именно. значение, количество и имя тега (которое берется с использованием идентификатора тега) в таблице. Для первых двух атрибутов я использую ngFor. Но я также хочу напечатать tagName, которое я получаю, используя tagId, и сохраняю его в массиве 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)
}
)
}
И вот как я пытаюсь напечатать их в пользовательском интерфейсе:
<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>
Но я получаю [объектный объект] под именем тега столбца. Может ли кто-нибудь помочь мне решить эту проблему?
Я попытался использовать ng-container, как указано выше, но результат выглядит следующим образом в пользовательском интерфейсе:
Что неправильно. Мне нужны только первые 3 строки с именами тегов, такими как «Subtag 3», «Zeit1», «Tag 1» для строк 1,2,3 соответственно.
Заранее спасибо.