简体   繁体   English

Angular2中的动态插值

[英]Dynamic Interpolation in Angular2

I am learning Angular-2 and try to build data-grid component and run into a problem with dynamic interpolation. 我正在学习Angular-2,并尝试构建数据网格组件并遇到动态插值问题。 Assume, the other component will consume the data-grid like the following: 假设其他组件将消耗数据网格,如下所示:

<data-grid [model] = 'users'>
   <column header='Last Name' value="lastName" ></column>
   <column header='First Name' value='firstName' ></column>
   <column header='Address' value='address' ></column>
</data-grid>

The data-grid component holds a collection of columns. 数据网格组件包含列的集合。 From the data-gird, I try to loop through the columns collection to build the column header then show all data. 从数据网格,我尝试遍历column集合以构建列标题,然后显示所有数据。 The headers for the table is easy but I don't know how to do the nested interpolation for row and columns. 该表的标题很简单,但我不知道如何对行和列进行嵌套插值。

<div class="container" >
    <table class="table table-hover">
         <thead class="thead-default">
             <tr>
                <th template="ngFor let col of columns">
                    {{ col.header}}
                 <th>
             </tr>
         </thead>
      <tbody>
          <tr template="ngFor let row of model">
               <td template="ngFor let col of columns">
                <!-- want to loop though the columns 
                {{row.{{col.value}}}} //How to make this statement works? -->
              </td>
          </tr>
      </tbody>  
    </table>
</div>

Any idea how to solve this problem? 任何想法如何解决这个问题?

thanks, 谢谢,

Austin 奥斯汀

You do not need the additional evaluation brackets. 您不需要其他评估支架。 Use square brackets for array indexing. 使用方括号进行数组索引。

<tr *ngFor="let row of model">
   <td *ngFor="let col of columns">
       {{row[col.value]}} 
   </td>
</tr>

http://plnkr.co/edit/DVwolRDu0JNcsXTaTrir http://plnkr.co/edit/DVwolRDu0JNcsXTaTrir

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM