【发布时间】: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