2016-08-12 60 views
0

我想自定义号码管角2 {{myvar |编号:'1.2-2'}} myvar = 1000 我得到1,000 我想要的是获得1 000 一个空间的位置, 任何想法?如何自定义号码管角2

+1

内置'number'格式化程序不支持,但文档https://angular.io/docs/ts/latest/guide/pipes.html描述了编写自己的自定义管道 –

回答

0

你可以连续使用管道。 {{ myvar | number:'1.2-2' | thousand }}和你的千管看​​起来像这样。

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

@Pipe({ 
    name: 'thousand' 
}) 

export class ThousandPipe implements PipeTransform { 
    transform(value: any, digits?: string): string { 
    if (value) { 
     let thousands = value.split(','); 
     const preDecimalValue = thousands.pop(); 
     thousands = thousands.join(' '); 
     return thousands + ' ' + preDecimalValue; 
    } 
    return ''; 
    } 
} 

假定所有的千位用逗号分隔,您将设置千位数组中的所有千位。在你的情况下,数千将是['1'],preDecimalValue将是'000.00'