例如,如何为 Angular 4 中的数字管道指定/覆盖默认(区域设置)千位分隔符?
{{p.total | number}}
?
角度 5+
从 Angular 5 开始,区域设置参数已添加到十进制管道中,如官方文档中所示:https://angular.io/api/common/DecimalPipe https://angular.io/api/common/DecimalPipe。这意味着您可以在调用管道时直接选择区域设置,例如:
{{p.total | number:'':'fr-FR'}}
请注意,这也会更改小数点分隔符。
角度 2+
或者如果您只想更改千位分隔符......
根据 Angular 关于 DecimalPipe 的文档:https://v2.angular.io/docs/ts/latest/api/common/index/DecimalPipe-pipe.html https://v2.angular.io/docs/ts/latest/api/common/index/DecimalPipe-pipe.html,没有可以添加到管道调用中以异常更改用于格式化的字符的显式参数。
如果您不想更改整个项目的区域设置或关联的默认值,我认为最好的办法是编写自己的管道来处理您的特殊情况。别担心,管道非常容易编写。
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'numberfr'
})
export class FrenchDecimalPipe implements PipeTransform {
transform(val: number): string {
// Format the output to display any way you want here.
// For instance:
if (val !== undefined && val !== null) {
return val.toLocaleString(/*arguments you need*/);
} else {
return '';
}
}
}
不要忘记将其添加到 NgModule 中才能使用它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)