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]

相关内容

热门资讯

透视总结!红龙poker透视工... 透视总结!红龙poker透视工具,pokernow辅助控制(详细辅助2025新版);红龙poker透...
wepoke辅助有挂!wpk辅... wepoke辅助有挂!wpk辅助器是真的假的(透视)一贯真的是有挂(详细辅助透视教程)1、这是跨平台...
透视能赢!德普之星辅助器app... 透视能赢!德普之星辅助器app,wepoker轻量版有透视吗(详细辅助解密教程)1、下载好德普之星辅...
wepoke确实有挂!wpk怎... wepoke确实有挂!wpk怎么提高胜率(透视)总是有挂(详细辅助解密教程)1)wepoke确实有挂...
透视脚本!aapoker透视脚... 透视脚本!aapoker透视脚本下载,pokerworld软件(详细辅助2025新版)1、下载好aa...
wepoke真的有挂!来玩德州... wepoke真的有挂!来玩德州app有挂吗(透视)素来真的有挂(详细辅助揭秘攻略)1)wepoke真...
透视教学!智星德州有脚本吗,智... 透视教学!智星德州有脚本吗,智星德州插件怎么下载(详细辅助线上教程)一、智星德州有脚本吗AI软件牌型...
微扑克ai辅助!wpk专用辅助... 微扑克ai辅助!wpk专用辅助程序(透视)竟然真的是有挂(详细辅助透明教程)是一款可以让一直输的玩家...
透视数据!德普之星辅助器,we... 透视数据!德普之星辅助器,wepoker辅助是真的假的(详细辅助力荐教程);德普之星辅助器辅助器中分...
wepokeai代打的胜率!w... wepokeai代打的胜率!wpk的发牌有规律吗(透视)其实真的是有挂(详细辅助专业教程)是一款可以...