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、首先打开wepoker可以...
玩家爆料!wejoker辅助器... 玩家爆料!wejoker辅助器怎么卖(辅助挂)好像是真的有挂(有挂教程)玩家爆料!wejoker辅助...
一秒答解!wepokerh5破... 一秒答解!wepokerh5破解(辅助挂)确实是真的有挂(有挂方法)1、进入到wepokerh5破解...
我来教大家!wepoker怎么... 我来教大家!wepoker怎么看牌型(辅助挂)一直是真的有挂(有挂头条)1)wepoker怎么看牌型...
一分钟揭秘!aapoker插件... 一分钟揭秘!aapoker插件下载(辅助挂)果然是有挂(有挂方针)1、起透看视 aapoker插件下...
教程攻略!wepoker免费透... 教程攻略!wepoker免费透视(辅助挂)确实是有挂(有挂透视)1、这是跨平台的wepoker免费透...
每日必看推荐!wepoker透... 每日必看推荐!wepoker透视底牌(辅助挂)一贯有挂(有挂实锤)运wepoker透视底牌辅助工具,...
必备教程!德普辅助软件(辅助挂... 必备教程!德普辅助软件(辅助挂)确实是有挂(有挂透视)1、德普辅助软件脚本辅助下载、德普辅助软件免费...
技术分享!wepoker有没有... 技术分享!wepoker有没有插件(辅助挂)果然是真的有挂(有挂存在)暗藏猫腻,小编详细说明wepo...
玩家必知教程!wpk软件是正规... 玩家必知教程!wpk软件是正规的吗(辅助挂)都是存在有挂(有人有挂)1、金币登录送、破产送、升级送、...