我可以从管道调用 Angular2 管道吗?

2024-01-27

我从服务器返回一个对象,其中包含一个列及其定义的数组和一个行数组。我想在构建 HTML 表时迭代列和行,并根据使用“格式”管道返回的列类型格式化单元格。

例如

WS 回应:

{
  "columns" [{"precision":10,"name":"PAGES","typeName":"INTEGER","scale":0...,
  "rows":[{"PAGES":6531....}, [{"PAGES":6531....}]
}

HTML 片段:

<tr *ngFor="let row of invoices?.rows">
  <td *ngFor="let column of invoices?.columns>
    {{row[column.name] | format : column}}
  </td>
</tr>

有什么方法可以让我的“格式”管道根据列的类型充当正确的内置管道(退出的地方)的委托人?我不想重新实现 DecimalPipe、DatePipe 等。


是的,你可以调用管道 - 只需实例化它们并调用transform:

transform(cell: any, column: any): any {
    if (column.typeName === "INTEGER") {
        let pipe = new DecimalPipe();
        return pipe.transform(cell, "1.0-0");
    }
    // more here ...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我可以从管道调用 Angular2 管道吗? 的相关文章

随机推荐