AngularmatDatepicker禁用日期转型
创始人
2024-10-28 07:01:37
0

要禁用matDatepicker中的日期转型,可以使用DateAdapter中的format方法。可以扩展NativeDateAdapter并覆盖format()。以下是一个示例:

import { NativeDateAdapter} from '@angular/material/core';
import { MatDateFormats } from '@angular/material/core';

export class MyDateAdapter 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 `${year}-${month}-${day}`;
    }
    return date.toDateString();
  }
}

export const MY_DATE_FORMATS: MatDateFormats = {
  parse: {
    //不需要设置解析格式;
    dateInput: null,
  },
  display: {
    dateInput: 'input',
    monthYearLabel: 'MM/YYYY',
    dateA11yLabel: 'LL',
    monthYearA11yLabel: 'MMMM YYYY',
  },
};

//添加到模块之后,可以使用它对matDatepicker的所有日期选项卡进行禁用。MatDatePicker中有一个“dateClass“选项,它可以使用matDatepicker的日历组件中的自定义函数来禁用特定日期。

@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.css'],
  providers: [
    { provide: DateAdapter, useClass: MyDateAdapter },
    { provide: MAT_DATE_FORMATS, useValue: MY_DATE_FORMATS },
  ],
})
export class MyComponent {}

//在模板上设置日期className样式来禁用它
template:
  
    
    
    
  

ts:
  dateClass() {
    return (date: Date): MatCalendarCellCssClasses => {
      const day = date.getDate();
      // Disable 1st of

相关内容

热门资讯

避坑细节!wepoker插件下... 避坑细节!wepoker插件下载(辅助挂)果然存在有挂(有挂细节辅助插件)1、这是跨平台的轻量版有透...
查到实测辅助!wepoker亲... 查到实测辅助!wepoker亲友圈有用吗(辅助挂)原来真的有挂(有挂讲解辅助插件)1、免费脚本咨询教...
分享一款!拱趴大菠萝开挂方法(... 分享一款!拱趴大菠萝开挂方法(辅助挂)都是真的有挂(存在有挂辅助攻略)1、实时透视辅助更新:用户可以...
科技介绍!黑侠破解wepoke... 科技介绍!黑侠破解wepoker(辅助挂)确实是真的有挂(有挂详细辅助方法)1、模拟器是什么优化,俱...
最新技巧!德普之星app安卓版... 最新技巧!德普之星app安卓版破解版(辅助挂)竟然存在有挂(真的有挂辅助教程)1、每一步都需要思考,...
玩家交流!wepoker有没有... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
解密关于!wepoker插件功... 解密关于!wepoker插件功能辅助器(辅助挂)本来有挂(详细教程辅助器)1、任何透视是真的假的的玩...
科技揭秘!wpk俱乐部辅助器(... 科技揭秘!wpk俱乐部辅助器(辅助挂)好像真的是有挂(发现有挂辅助工具)1、全新机制【ai辅助工具激...
热点推荐!fishpoker透... 热点推荐!fishpoker透视(辅助挂)一直是真的有挂(有人有挂辅助app)1、不需要AI权限,帮...
让我来分享经验!xpoker辅... 让我来分享经验!xpoker辅助器(辅助挂)原来是真的有挂(的确有挂辅助攻略)1、每一步都需要思考,...