【问题标题】:Can I call an Angular2 pipe from a pipe?我可以从管道中调用 Angular2 管道吗?
【发布时间】:2016-11-16 13:57:15
【问题描述】:

我从服务器返回一个对象,该对象包含一组列及其定义和一组行。我想在构建 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 等。

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

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

    transform(cell: any, column: any): any {
        if (column.typeName === "INTEGER") {
            let pipe = new DecimalPipe();
            return pipe.transform(cell, "1.0-0");
        }
        // more here ...
    }
    

    【讨论】:

    • 辛苦了,谢谢。我必须做的唯一 mod 是注入 LOCALE_ID 并将其传递给十进制管道构造函数。
    猜你喜欢
    • 1970-01-01
    • 2020-07-31
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 2017-04-10
    • 2021-04-23
    • 1970-01-01
    相关资源
    最近更新 更多