你可以延长
CurrencyPipe
import { Pipe } from '@angular/core';
import { CurrencyPipe } from '@angular/common';
@Pipe({
name: 'usdCurrency'
})
export class UsdCurrencyPipe extends CurrencyPipe {
transform(value: number, digitsInfo: string = "1.0-0"): any {
return super.transform(value, "USD", "symbol", digitsInfo); }
}
并在模板中应用该自定义管道:
<p>{{amount1 | usdCurrency }}</p>
<p>{{amount2 | usdCurrency }}</p>
<div>{{amount3 | usdCurrency }}</div>
<h1>{{amount4 | usdCurrency : "1.2-2" }}</h1>
在上面的例子中,
digitsInfo
可用作可选参数。如果需要,可以提供其他参数(
currencyCode
,
display
locale
).
见
this stackblitz