【问题标题】:How to create a pipe to format account numbers?如何创建管道来格式化帐号?
【发布时间】:2023-01-26 19:09:11
【问题描述】:

有没有Angular方面的高手,能花点时间给我解释一下,如何创建一个管道来格式化账号?

帐号的长度始终为 12 位数字。

例如 => 123456789123

  • 从 webService 中检索到的变量是 number 类型的。

  • 我想要这种类型的显示器

123-4567891-23
458-4698712-15
etc...

我还是初学者,无法弄清楚我应该如何创建它。有人会好心帮助我创建管道,所以我可以理解。

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'accountDash'
})
export class AccountDashPipe implements PipeTransform {
  transform(): any {
    return;
  }
}

谢谢你的帮助,如果我的信息太轻,我深表歉意。

【问题讨论】:

  • transform(yourInput:string) {///do the stufff with yourInput}

标签: angular typescript


【解决方案1】:

您可以将数字转换为字符串并很容易地对其进行格式化

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'accountDash'
})
export class AccountDashPipe implements PipeTransform {
  transform(accountNumber: Number): string {
    return String(accountNumber).replace(/^(d{3})(d{7})(d{2})$/, `$1-$2-$3`)
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    相关资源
    最近更新 更多