相关疑难解决方法(0)

Angular 9 - 如何扩展(本地环境感知周开始)NativeDateAdapter 工作?

为了了解语言环境(关于本周开始)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)

angular-material angular

5
推荐指数
1
解决办法
570
查看次数

Angular 9 如何使用/导入 DateAdapter、NativeDateAdapter“无法解析‘@angular/material/core/datetime’”

我正在尝试为日期选择器设置正确的(基于语言环境的)一周开始。所以我使用了这个那个问题并实现了我自己的 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)

angular-material angular

1
推荐指数
1
解决办法
2246
查看次数

标签 统计

angular ×2

angular-material ×2