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到底有没有透视这款游戏可以开挂的,确实是有挂的,需要了解加去威信【4852750...
透视透视!wepoker怎么开... 透视透视!wepoker怎么开辅助(透视)佛手大菠萝13道挂哪里,教程攻略(真是有挂)-哔哩哔哩1、...
透视透视!hhpoker真的有... 透视透视!hhpoker真的有透视吗(透视)pokermaster破解版,教程策略(确实有挂)-哔哩...
透视揭露!wepoker能不能... 透视揭露!wepoker能不能透视(透视)wepoker免费透视脚本,教程窍要(有挂详细)-哔哩哔哩...
透视解迷!wpk刷入池率脚本(... 透视解迷!wpk刷入池率脚本(透视)wepoker高级辅助,教程技法(真的有挂)-哔哩哔哩1、玩家可...
透视推荐!wepoker亲友圈... 透视推荐!wepoker亲友圈有用吗(透视)wepoker透视有没有,教程教材(有挂详细)-哔哩哔哩...
透视分享!hhpoker智能辅... 透视分享!hhpoker智能辅助插件(透视)有没有人wepoker,教程教程书(真实有挂)-哔哩哔哩...
透视推荐!pokermaste... 透视推荐!pokermaster脚本(透视)佛手在线大菠萝技巧,教程手段(有挂规律)-哔哩哔哩1、很...
透视分享!哈糖大菠萝开挂(透视... 透视分享!哈糖大菠萝开挂(透视)wpk私人局辅助是真的吗,教程练习(有挂技术)-哔哩哔哩1、这是跨平...
透视关于!newpoker怎么... 透视关于!newpoker怎么安装脚本(透视)大菠萝免费辅助,教程烘培(有挂详细)-哔哩哔哩1、超多...