【发布时间】:2018-10-20 23:44:06
【问题描述】:
我有一个表格组件,它采用输入 JSON 作为表格格式,采用另一个 JSON 作为数据。我的问题是,当我的表格使用 *ngFor 渲染时,如何调用 cellFunction?
我的表格格式 JSON:
tblFormat= [
{ headerTxt: 'Order ID', df: 'Order_ID', color: 'blue', cellFunction: 'testME1' },
{ headerTxt: 'Buyer Name', df: 'name', color: 'blue',cellFunction: 'testME2' }
]
我的组件
import { Component, AfterViewInit, ViewChild, Input } from '@angular/core';
@Component({
selector: 'table-comp',
template: `<table class="table table-hover">
<thead>
<tr>
<th *ngFor="let h of tableInfo" [style.color]="h.color">{{h.headerTxt}}</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let d of data">
<td *ngFor="let c of tableInfo" [style.color]=" how do I? c.cellFunction()">
{{d[c.df]}}
</td>
</tr>
</tbody>
</table>`
inputs: ['data','tableInfo']
})
export class TableComp {
tableInfo=[];
data=[];
}
【问题讨论】:
-
您想调用
c.cellFunction()使用[style.color]分配颜色? -
什么是cellFunction?它是一个函数,还是如图所示的字符串?
-
c[cellFunction]()如果你想打电话给testME1() -
@baao 我假设该函数是在他的组件中定义的。我不认为单元格具有 Function 作为属性,尽管它可以!
-
哈哈。是啊,你说得对。最后看起来真的很奇怪...... :-) @sabithpocker。
标签: javascript angular angular2-directives angular2-components