为了了解语言环境(关于本周开始)Material DatePicker 我创建了这个扩展:
import { Injectable } from "@angular/core";
import { LOCALE_ID, Inject } from "@angular/core";
import { Platform } from "@angular/cdk/platform";
import { getLocaleFirstDayOfWeek } from "@angular/common";
import { NativeDateAdapter } from '@angular/material/core';
@Injectable({providedIn: 'root'})
export class LocaleDateAdapter extends NativeDateAdapter {
constructor(@Inject(LOCALE_ID) public locale: string) {
super(locale, new Platform());
}
getFirstDayOfWeek() {
return getLocaleFirstDayOfWeek(this.locale);
}
}
Run Code Online (Sandbox Code Playgroud)
我还尝试使用 0 args 构造函数并不断重新调整 1 作为一周的第一天。一些同事说这{providedIn: 'root'}可能会有所帮助——但没有。
我把它app.module.ts作为提供者连接起来:
{
provide: DateAdapter,
useClass: LocaleDateAdapter
}
Run Code Online (Sandbox Code Playgroud)
我的 DatePicker 设置如下:
<mat-form-field appearance="fill"> …Run Code Online (Sandbox Code Playgroud) 我正在尝试为日期选择器设置正确的(基于语言环境的)一周开始。所以我使用了这个和那个问题并实现了我自己的 DateAdapter:
import { NativeDateAdapter } from "@angular/material/core/datetime";
import { Injectable } from "@angular/core";
import { LOCALE_ID, Inject } from "@angular/core";
import { Platform } from "@angular/cdk/platform";
import { getLocaleFirstDayOfWeek } from "@angular/common";
@Injectable()
export class LocaleDateAdapter extends NativeDateAdapter {
constructor(@Inject(LOCALE_ID) public locale: string) {
super(locale, new Platform());
}
getFirstDayOfWeek() {
return getLocaleFirstDayOfWeek(this.locale);
}
}
Run Code Online (Sandbox Code Playgroud)
我也相应地将它放入提供者中:
import { LocaleDateAdapter } from './components/shared-components/locale-date-adapter';
import { DateAdapter } from '@angular/material/core/datetime';
...
providers: [
...
{
provide: DateAdapter,
useClass: …Run Code Online (Sandbox Code Playgroud)