如何使用 ngFor 指令将子 ID 传递给 Angular 2 中的父对象
how to pass child id to parent object in Angular 2 using ngFor directive
如何在 Angular 2 中使用 ngFor 指令将子 ID 传递给父对象。
我需要实现动态 td 和 tr(使用 Angular 2 beta.17)。
我试过下面的代码,但它不起作用。
错误:
模板解析错误:
词法分析器错误:表达式 [aItem['{{header.id]
中第 18 列的未终止引号
数据:
headerList:[
{"id":"id1", "label":"ID1"},
{"id":"id2", "label":"ID2"},
{"id":"id3", "label":"ID3"},
]
resp =[
{
"id1": "1",
"id2": "1",
"id3": "1",
"id4": "1",
"id5": "1"
},
{
"id1": "2",
"id2": "2",
"id3": "2",
"id4": "2",
"id5": "2"
},
{
"id1": "3",
"id2": "3",
"id3": "3",
"id4": "3",
"id5": "3"
},
{
"id1": "4",
"id2": "4",
"id3": "4",
"id4": "4",
"id5": "4"
}
]
代码:
<table class="table table-striped">
<thead>
<tr>
<th *ngFor="let header of headerList">
{{header.label}}
</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let aItem of resp">
<td *ngFor="let header of headerList">
{{aItem['{{header.id}}']}}
</td>
</tr>
</tbody>
</table>
您不能在 {{}}
中使用 {{}}
。我不确定您实际尝试完成什么,但这可能是您想要的:
{{aItem[header.id]}}
如何在 Angular 2 中使用 ngFor 指令将子 ID 传递给父对象。
我需要实现动态 td 和 tr(使用 Angular 2 beta.17)。 我试过下面的代码,但它不起作用。
错误:
模板解析错误: 词法分析器错误:表达式 [aItem['{{header.id]
中第 18 列的未终止引号数据:
headerList:[
{"id":"id1", "label":"ID1"},
{"id":"id2", "label":"ID2"},
{"id":"id3", "label":"ID3"},
]
resp =[
{
"id1": "1",
"id2": "1",
"id3": "1",
"id4": "1",
"id5": "1"
},
{
"id1": "2",
"id2": "2",
"id3": "2",
"id4": "2",
"id5": "2"
},
{
"id1": "3",
"id2": "3",
"id3": "3",
"id4": "3",
"id5": "3"
},
{
"id1": "4",
"id2": "4",
"id3": "4",
"id4": "4",
"id5": "4"
}
]
代码:
<table class="table table-striped">
<thead>
<tr>
<th *ngFor="let header of headerList">
{{header.label}}
</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let aItem of resp">
<td *ngFor="let header of headerList">
{{aItem['{{header.id}}']}}
</td>
</tr>
</tbody>
</table>
您不能在 {{}}
中使用 {{}}
。我不确定您实际尝试完成什么,但这可能是您想要的:
{{aItem[header.id]}}