2016-06-09 61 views
1

Plunker* ng对于阵列中的两个值

我想用角度使用管道格式化数据。对于标题和字段名称,我使用从typescript传入的数组,它工作得非常好。问题是当我尝试从同一个数组传递字段类型时,角度不接受它。

错误是:

Error: Uncaught (in promise): Template parse errors: 
Parser Error: Unexpected token '[' at column 23 in [{{tran[col.field] | tran[col.type]}}] in [email protected]:79 
      ("et tran of _trans"><td class="text-navy" *ngFor="let col of cols">[ERROR ->] 
    {{tran[col.field] | tran[col.type]}}</td> 

打字稿代码:

private cols; 
    this.cols = [ 
      { field: "DateCreated", header: "Date", type: "date" }, 
      { field: "Name", header: "Customer", type: "string" }, 
      { field: "Amount", header: "Amount", type : "money" } 
        ]; 

Angular2代码:

<thead> 
            <tr> 
             <th *ngFor="let col of cols">{{col.header}}</th> 
            </tr> 
           </thead> 
           <tbody> 
            <tr *ngFor="let tran of _trans"> 
             <td class="text-navy" 
           *ngFor="let col of cols"> 
           {{tran[col.field] 
           | col.type <--- here's the problem!}} 
           </td> 
           </tr> 
           </tbody> 

编辑:我只是试图{{TRAN [col.field] + col.type}}不给出错误,它显示我的数据类型名称和值。问题从我放入|时开始在中间..

+0

为什么不整'col'对象传递给管?然后做类型检查那里... – Sasxa

+0

希望我会知道如何做到这一点......你可以请一个小样本? – Ezi

+0

下面是将对象传递给管道的示例 - [plunker](http://plnkr.co/edit/l5AVp7TqI2vnjt7yDTxe?p=preview) – Sasxa

回答

0

更新

如果你想通过提供数据通道,这样的事情应该工作:

<td class="text-navy" 
    *ngFor="let col of cols"> 
    {{col.type.tranform(tran[col.field])}}> 

你缺少管向管道添加名称和参数:

{{value | pipe:param}} 

这也适用

{{value | pipe:param[propName]}} 
+0

我不会错过它,我提供它在数组中,它应该加载相同的数据加载。 – Ezi

+0

您无法从阵列传递管道。管道名称需要静态添加到你的模板中,至少如果你想使用'|管道“语法。管道只是代码,你总是可以从绑定中调用代码。 –

+0

哦,这正是我的问题..有没有解决办法?我不想列出模板中的所有字段..它更清晰,在列的列表上有ngFor ...仍然需要格式化一些列。 – Ezi