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

相关内容

热门资讯

一分钟了解!hh poker透... 一分钟了解!hh poker透视器下载,aapoker辅助工具安全吗,揭秘教程(一直真的有挂)-哔哩...
透视有挂!hhpoker德州挂... 【福星临门,好运相随】;透视有挂!hhpoker德州挂真的有吗,werplan透视挂,2026新版(...
程序员教你!hhpoker德州... 程序员教你!hhpoker德州机器人,hhpoker德州辅助,技巧教程(一贯真的是有挂)-哔哩哔哩;...
玩家必看教程!wpk是真的还是... 玩家必看教程!wpk是真的还是假的,wepoker透视脚本,透视教程(一贯确实有挂)-哔哩哔哩;亲,...
发现一款!wepokerplu... 发现一款!wepokerplus开挂,wpk官网下载链接,辅助教程(一直真的是有挂)-哔哩哔哩;1、...
揭秘几款!hhpoker可以控... 揭秘几款!hhpoker可以控制牌吗,wepoker透视脚本是什么,攻略教程!(其实是有挂的)-哔哩...
一分钟快速了解!wejoker... 一分钟快速了解!wejoker透视方法,wepoker线上大神,必备教程(果然确实有挂)-哔哩哔哩;...
一分钟了解!hhpoker免费... 一分钟了解!hhpoker免费透视脚本,pokemmo辅助器,透牌教程(一贯确实有挂)-哔哩哔哩;1...
透视规律!wepoker怎么买... 透视规律!wepoker怎么买辅助,wepoker分析,透牌教程(原来有挂)-哔哩哔哩;1.wepo...
我来向大家传授!wejoker... 我来向大家传授!wejoker开挂,竞技联盟辅助插件,分享教程(原来真的是有挂)-哔哩哔哩;竞技联盟...