角度为2的数千个分离器

Dan*_*iel 6 typescript ionic2 angular

我在我的proyect中试过这个:

<div padding *ngIf="TotalAPagar">
    $ {{TotalAPagar | number }}  
</div>
Run Code Online (Sandbox Code Playgroud)

调用我的变量TotalAPagar,我正在使用数字管道,但它显示的值为1,000,500.

我需要将编号约定更改为dots.例如.1.000.000

我一直在文档中阅读角度,但没有任何例子.

Jar*_* K. 6

我认为有两种方法可以解决这个问题:


1.您可以尝试从@angular/common库覆盖DecimalPipe:

这样:

点替换器.pipe.ts

import { Pipe } from '@angular/core';
import {DecimalPipe} from "@angular/common";

@Pipe({
    name: 'pointReplacer'
})
export class PointReplacerPipe extends DecimalPipe {

  transform(value: any, digits?: string): string {
        return super.transform(value, digits).replace(',', '.');


    }
}
Run Code Online (Sandbox Code Playgroud)

并在您的html 代码中:

<div padding *ngIf="TotalAPagar">
    $ {{TotalAPagar | pointReplacer }}  
</div>
Run Code Online (Sandbox Code Playgroud)

2.您可以编写自己的管道,它替换字符并在html代码中使用**双管道**

尝试这样:

点替换器.pipe.ts

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

@Pipe({
    name: 'pointReplacer'
})

export class PointReplacerPipe implements PipeTransform {
    transform(value: string, args: any[]): string {
        if(value) {
          return value.replace(',', '.');
        }
        return '';
    }
}
Run Code Online (Sandbox Code Playgroud)

并在您的html 代码中:

<div padding *ngIf="TotalAPagar">
    $ {{TotalAPagar | number | pointReplacer }}  
</div>
Run Code Online (Sandbox Code Playgroud)

无论您选择哪种方法,都不要忘记在模块中使用它时声明您自己的管道:

@NgModule({
  declarations: [PointReplacerPipe],
  providers: [PointReplacerPipe]
}) 
Run Code Online (Sandbox Code Playgroud)


Dan*_*iel 4

\xe2\x80\x8b在阅读有关 Angular js 和 javascript 的论坛和文档后,我发现了一种将数字放入格式和货币的方法,该方法是 toLocaleString(),是 javascript 中的一种方法,有助于将其放入货币或您需要的语言。

\n\n

我用该方法搜索我需要的国家/地区的名称,并向我显示所需的信息。(http://www.localeplanet.com/icu/es-CO/),就我而言是哥伦比亚。

\n\n

在我的函数中,我只需将 .toLocaleString(\'es-CO\') 添加到结果中,并将该值与指定的货币一起放置。

\n\n

例如:

\n\n
\n

this.TotalAPagar =\n (this.calcularDescuentosLiquidacion(this.TotalDevengadoValor,\n this.sueldoPendientePorCancelarValor, this.item.libranza,\n this.item.prestamo_empleado)+\n this.calularIndemnizacion(this.item.promedio_salario,\n this.item.fecha_fin_contrato,\n this.item.fecha_inicio_contrato)).toLocaleString(\'es-CO\');

\n
\n