我在app.module中添加以下代码,以便将en-AU语言环境设置为默认值:
import { registerLocaleData } from '@angular/common';
import localeAu from '@angular/common/locales/en-AU';
registerLocaleData(localeAu);
@NgModule({
providers: [
{ provide: LOCALE_ID, useValue: 'en-AU' },
],
})
我把它注入我的组件:
fee = 20.0
constructor(
@Inject(LOCALE_ID) public locale: string) {
}
当我尝试查看组件中的区域设置时:
<h1>Current locale: {{ locale }}</h1>
<h1>Fee default: {{ fee | currency }}</h1>
<h1>Fee AUD: {{ fee | currency:'AUD' }}</h1>
我看到以下内容:
Current locale: en-AU
Fee default: USD20.00
Fee AUD: $20.00
为什么默认费用不是20美元?
发现很奇怪的东西:
根据documentation管道中display
属性的Currency
默认值是symbol
,由于一些奇怪的原因是'AUD'。如果要显示$ sign,请将代码更改为:
<h1>Fee AUD: {{ amount | currency:null:'symbol-narrow' }}</h1>
现在,对于奇怪的部分,我创建了stackblitz进行演示。它与您描述的问题相同。但是,如果我更改我的代码
registerLocaleData(localeAu);
至
registerLocaleData(localeAu, 'AU');
代码工作正常,符号USD替换为$。