我正在使用 Angular8 并想格式化日期和时间,但我必须一次又一次地使用具有相同格式模式的日期管道,如下所示
<p>{{ myDate | date: 'dd MMM yyyy, h:mm a' }}</p>
<p>{{ myOtherDate date: 'dd MMM yyyy, h:mm a' }}</p>
<p>{{ otherVar }}</p>
<p>{{ myOtherOtherDate date: 'dd MMM yyyy, h:mm a' }}</p>
date: 'dd MMM yyyy, h:mm a'
并且在我的项目的每个组件中都将使用相同的格式。
有没有一种方法可以在我的应用模块的提供者中提供全局配置,就像这样
@NgModule({
providers: [
{provide: LOCAL_PIPE_DATE_PATTERN, useValue: 'ddMMyy'},
],
})
export class AppModule {}
并且只需使用日期管道作为
<p>{{ myDate | date }}</p>
<p>{{ myOtherDate | date }}</p>
<p>{{ otherVar }}</p>
<p>{{ myOtherOtherDate | date:'MMyyDD' }}</p> (If format changed)