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

相关内容

热门资讯

透视普及"来玩德州破... 透视普及"来玩德州破解器"本来有辅助方法(哔哩哔哩)暗藏猫腻,小编详细说明来玩德州破解器破解器有用吗...
透视专业"hhpok... 透视专业"hhpoker免费辅助器"都是真的是有辅助插件(哔哩哔哩)1、起透看视 hhpoker免费...
透视必备"wepok... 透视必备"wepoker怎么发冤家牌"本来真的有辅助器(哔哩哔哩)1、该软件可以轻松地帮助玩家将we...
透视解迷"aapok... 您好,aapoker怎么拿好牌这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054...
透视曝光"we po... 透视曝光"we poker免费辅助器"一贯存在有辅助技巧(哔哩哔哩)we poker免费辅助器脚本下...
透视透视"wepok... 透视透视"wepoker有辅助插件吗"真是存在有辅助攻略(哔哩哔哩)一、wepoker有辅助插件吗可...
透视必备"拱趴大菠萝... 透视必备"拱趴大菠萝挂哪里"竟然是有辅助方法(哔哩哔哩)1、首先打开拱趴大菠萝挂哪里辅助器下载最新版...
透视必备"uupok... 您好,uupoker有透视吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】...
透视揭露"wepok... 透视揭露"wepoker代打辅助机器人"一贯有辅助教程(哔哩哔哩)1.wepoker代打辅助机器人 ...
透视解密"hhpok... 透视解密"hhpoker软件可以玩吗"原来是有辅助脚本(哔哩哔哩)透视解密"hhpoker软件可以玩...