Angularmat日期选择器手动输入日期无法正常工作。
创始人
2024-10-28 12:01:17
0
  1. 在app.module.ts文件中导入DateAdapter和MAT_DATE_FORMATS,并将其添加到providers中:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatNativeDateModule, DateAdapter, MAT_DATE_FORMATS } from '@angular/material/core';
import { AppDateAdapter, CUSTOM_DATE_FORMATS } from './date.adapter';

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule,
    BrowserAnimationsModule,
    MatDatepickerModule,
    MatNativeDateModule
  ],
  providers: [
    { provide: DateAdapter, useClass: AppDateAdapter },
    { provide: MAT_DATE_FORMATS, useValue: CUSTOM_DATE_FORMATS }
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule { }
  1. 创建一个自定义日期格式的date.adapter.ts文件:
import { NativeDateAdapter } from '@angular/material/core';
import { MAT_DATE_FORMATS } from '@angular/material/core';
import { Injectable } from '@angular/core';

@Injectable()
export class AppDateAdapter extends NativeDateAdapter {
  format(date: Date, displayFormat: Object): string {
    if (displayFormat === 'input') {
      const day = date.getDate();
      const month = date.getMonth() + 1;
      const year = date.getFullYear();
      return `${month}/${day}/${year}`;
    } else {
      return date.toDateString();
    }
  }
}

export const CUSTOM_DATE_FORMATS = {
  parse: {
    dateInput: { month: 'short', year: 'numeric', day: 'numeric' }
  },
  display: {
    dateInput: 'input',
    monthYearLabel: { year: 'numeric', month: 'short' },
    dateA11yLabel: { year: 'numeric', month: 'long', day: 'numeric' },
    monthYearA11yLabel: { year: 'numeric', month: 'long' }
  }
};
  1. 在日期选择器的HTML代码中添加matDatepickerInput和[datepicker]

相关内容

热门资讯

透视有挂!wepoker透视脚... 透视有挂!wepoker透视脚本苹果版,竞技联盟透视插件,教材教程(都是真的有挂)-哔哩哔哩1、不需...
三分钟法门!越乡有辅助软件,四... 三分钟法门!越乡有辅助软件,四川途游辅助软件下载,揭秘教程(有挂技巧)-哔哩哔哩1、起透看视 四川途...
透视黑科技!线上德州的辅助器是... 透视黑科技!线上德州的辅助器是什么(透视)开挂辅助攻略(切实真的有挂)-哔哩哔哩1、玩家可以在线上德...
透视能赢!pokernow辅助... 透视能赢!pokernow辅助控制,wepoker有没有辅助,阶段教程(本来真的有挂)-哔哩哔哩1、...
九分钟窍门!乐玩游戏辅助工具,... 九分钟窍门!乐玩游戏辅助工具,新芒果监利开机贴吧,黑科技教程(有挂方法)-哔哩哔哩小薇(透视辅助)致...
透视线上!wepoker有没有... 透视线上!wepoker有没有透视方法(透视)开挂辅助方法(原来有挂)-哔哩哔哩1、进入游戏-大厅左...
九分钟策略!途游游戏辅助软件,... 九分钟策略!途游游戏辅助软件,顺欣茶楼有没有辅助,系统教程(有挂细节)-哔哩哔哩1、全新机制【顺欣茶...
透视透视!aapoker怎么开... 透视透视!aapoker怎么开辅助器,aapoker ai插件,演示教程(原来是真的挂)-哔哩哔哩1...
透视线上!sohoopoker... 透视线上!sohoopoker辅助(透视)开挂辅助插件(好像存在有挂)-哔哩哔哩1、任何sohoop...
第1分钟模板!指尖四川辅助,同... 第1分钟模板!指尖四川辅助,同城乐吧乐享版510k辅助,技巧教程(有挂方式)-哔哩哔哩1)同城乐吧乐...