【问题标题】:how to pass child id to parent object in Angular 2 using ngFor directive如何使用ngFor指令将子ID传递给Angular 2中的父对象
【发布时间】:2016-05-02 13:17:28
【问题描述】:

如何在 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> 

【问题讨论】:

  • 哪个ID应该去​​哪里。你有一个标题列表,应该在正文中使用哪一个?

标签: angular typescript1.5


【解决方案1】:

您不能在{{}} 中使用{{}}。我不确定您实际尝试完成什么,但这可能是您想要的:

  {{aItem[header.id]}}

【讨论】:

  • 很高兴听到。如果这解决了您的问题,您是否可以通过单击 up/downvote 按钮下方的白色复选标记来接受答案以使您的问题得到回答?
猜你喜欢
  • 2017-03-23
  • 1970-01-01
  • 2021-04-26
  • 2013-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
相关资源
最近更新 更多