本文介绍了Angular 5-CurrencyPipe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对Angular中已内置的CurrencyPipe遇到问题.
I have a problem with the already built in CurrencyPipe from Angular.
我需要使用CurrencyPipe显示货币符号,但是除非提供输入数字,否则我将无法使用它.
I need to display a currency sign using the CurrencyPipe but I can't use it unless I provide an input number.
因为CurrencyPipe使用当前语言环境来获取货币符号,所以我认为输入数字可能是可选的.
Because the CurrencyPipe uses the current locale to get the currency sign, I was thinking that the input number could be optional.
当前行为:
{{ 1 | currency:'USD' }} --> $1
所需的行为:
{{ null | currency:'USD' }} --> $
您是否知道默认管道是否可以做到这一点?谢谢!
Does any of you know if this is possible with the default Pipe?Thanks!!
推荐答案
更新Angular 8
import { Pipe, PipeTransform } from '@angular/core';
import { CurrencyPipe } from '@angular/common';
@Pipe({ name: 'CustomeCurrency' })
export class CustomCurrencyPipe implements PipeTransform {
constructor(private currencyPipe: CurrencyPipe) { }
transform(value: any, currencyCode?: string, display?: string | boolean, digitsInfo?: string, locale?: string): string {
if (value != null)
return this.currencyPipe.transform(value, currencyCode, display, digitsInfo, locale);
return this.currencyPipe.transform(0, currencyCode, display, locale).split('0.00')[0];
}
}
尝试使用这个简单的自定义货币管道
Try this simple custom currency pipe
{{ null | CustomeCurrency }}</p>
import { Pipe, PipeTransform } from '@angular/core';
import { CurrencyPipe } from '@angular/common';
@Pipe({ name: 'CustomeCurrency' })
export class CustomCurrencyPipe implements PipeTransform {
constructor(private currencyPipe: CurrencyPipe) { }
transform(value: any, currency: string, symbol: boolean = false): string {
if (value != null)
return this.currencyPipe.transform(value, currency, symbol);
return this.currencyPipe.transform(0, currency, symbol).split('0.00')[0];
}
}
这篇关于Angular 5-CurrencyPipe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!