【问题标题】:How to modify date pipe in angular?如何以角度修改日期管道?
【发布时间】:2020-01-07 13:50:45
【问题描述】:

我遇到了一个问题 InvalidPipeArgument: for safari for time format 但感谢InvalidPipeArgument: '2017-12-05 05:30:00 for pipe 'DatePipe' - Safari 中的解决方案,我已经解决了这个问题

现在我正在使用:

<mat-cell *cdkCellDef="let payroll"
                  fxFlex="20%"> {{payroll.time | date : 'dd MMM y h:mm:ss a'}}</mat-cell>

在 ts 文件中,我将时间字符串转换为解决方案帖子中的时间字符串:

this.cashOutPayrollTableDataSource.data.forEach(key => {
          key.time = key.time.replace(/\s/g, 'T') + 'Z';
        });

但我需要在管道中制作这个 ts 文件代码。我使用的默认管道已经是 date pipe,它已经在 angular 中定义了。

如何将此打字稿代码移动到新的自定义管道代码中?

【问题讨论】:

    标签: javascript html angular typescript


    【解决方案1】:

    如果我理解正确,您想创建一个自定义管道吗?开始

    ng generate pipe myModuleName.myCustomPipe
    

    将您的逻辑放入管道的transform 方法中,如下所示:

    transform(value: string) {
        return value.replace(/\s/g, 'T') + 'Z';
    }
    

    并在模板中使用它

    {{ someItem.time | myCustomPipe }}
    

    您还可以链接管道:

    {{ someItem.time | myCustomPipe | date }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 2019-01-18
      • 2022-01-26
      • 2021-12-05
      • 1970-01-01
      • 2019-01-24
      相关资源
      最近更新 更多