【问题标题】:Angular Material mat-datepicker (change) event and formatAngular Material mat-datepicker(更改)事件和格式
【发布时间】:2022-02-15 02:42:54
【问题描述】:

我正在使用 angular material datepicker 指令,但我遇到的问题很少。

1.当我打开日期选择器对话框并选择任何日期时,日期会显示在输入文本框中,但我想在输入文本框中发生任何更改时调用一个函数。

现在,如果我在输入文本框中手动输入任何值,我正在使用 (input) 指令触发函数,它工作正常,如代码所示。我想在使用日期选择器对话框更改日期时触发相同的功能。

2. 我还想将日期格式从 mm/dd/yyyy 更改为 dd/mm/yyyy日期选择器对话框。这里指令中默认设置了 mm/dd/yyyy。

<input matInput [matDatepicker]="organizationValue" formControlName="organizationValue" (input)="orgValueChange(i)">
<mat-datepicker-toggle matSuffix [for]="organizationValue"></mat-datepicker-toggle>                 
<mat-datepicker #organizationValue></mat-datepicker>

【问题讨论】:

  • 请点击对您最有帮助的答案左侧的复选标记来关闭您的问题

标签: angular angular-material angular5


【解决方案1】:

来自docs 您可以根据您的要求使用以下事件之一

@Output()
dateChange(): EventEmitter<MatDatepickerInputEvent<D>>

在此触发更改事件时发出。

@Output()
dateInput(): EventEmitter<MatDatepickerInputEvent<D>>

在此触发输入事件时发出。

例如:

<input matInput #ref [matDatepicker]="organizationValue" formControlName="organizationValue" (dateChange)="orgValueChange(ref.value)">


 <input matInput #ref [matDatepicker]="organizationValue" formControlName="organizationValue" (dateInput)="orgValueChange(ref.value)">

【讨论】:

  • 您能否详细说明如何在 ts 文件中使用它。我应该与这些发射器一起创建这些函数吗?
  • @UmairJameel MatDatePicker 提供这些事件。我已经更新了我的回答
  • 我也面临同样的问题
【解决方案2】:
  1. 在您的 html 中,您可以使用 (ngModelChange)="functionName()" 来触发任何更改日期的函数,并在您的 ts.xml 中声明该函数。

  2. 更改日期格式:

将此添加到app.module.ts

import{MatDateFormats, MAT_DATE_FORMATS, NativeDateAdapter, DateAdapter} from '@angular/material';

const MY_DATE_FORMATS = {
    parse: {
        dateInput: { day: 'numeric', month: 'numeric', year: 'numeric' }
    },
    display: {
        dateInput: 'input',
        monthYearLabel: { year: 'numeric', month: 'short' },
        dateA11yLabel: { year: 'numeric', month: 'long', day: 'numeric' },
        monthYearA11yLabel: { year: 'numeric', month: 'long' },
    }
 };

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

app.module.ts的提供者中添加以下内容:

{provide: DateAdapter, useClass: AppDateAdapter},  
{provide: MAT_DATE_FORMATS, useValue: MY_DATE_FORMATS}

【讨论】:

  • 关于 ngChangeControl 的提示,我认为这应该被标记为这个问题的答案。我
  • 我认为这是关于格式化的第二个问题的正确答案
  • 它在正常模式下工作正常,但是当我在 prod 模式下构建项目时会抛出错误。 '错误:错误:内部错误:未知标识符 {"parse":{"dateInput":{"month":"numeric","year":"numeric","day":"numeric"}},"display ":{"dateInput":"input"}}'
【解决方案3】:

这就是我所做的,Angular 9:

html:

  <input
    matInput
    [matDatepicker]="picker"
    (dateChange)="onDateChange()"
  />

ts:

@Output()
dateChange: EventEmitter<MatDatepickerInputEvent<any>> = new EventEmitter();

onDateChange(): void {
    this.dateChange.emit();
}

【讨论】:

    【解决方案4】:

    HTML:

    <div class="someClass">
       <mat-form-field appearance="outline">
          <mat-label>Label</mat-label>
          <input (dateChange)="onDateChange($event)" formControlName="formControlName" matInput [matDatepicker]="picker">
          <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
          <mat-datepicker #picker></mat-datepicker>
       </mat-form-field>
    </div>
    

    TS:

      public onDateChange(event: MatDatepickerInputEvent<Date>): void {
        console.log('Teste', event.value);
      }
    

    【讨论】:

      【解决方案5】:

      MatDatePicker 似乎存在缺陷,因此如果您先清除日期(如果有值),转到另一个字段,然后使用手动输入编辑该字段(不使用日期选择器),然后离开该字段,月份和日期被切换。为了纠正这种行为,除了 AppDateAdapter 类中的“格式”方法之外,您还必须定义“解析”方法(参见 Rak2018 的答案):

      parse(value: any): Date | null {
          const vals = value.split('.'); // or '-' if you use - as separator
          if (vals.length !== 3 || parseInt(vals[2] < 1900) return null;
          return new Date(parseInt(vals[2]), parseInt(vals[1]) - 1, parseInt(vals[0]));
      }
      

      我所说的不足是指通常情况下,您可以在提供程序中定义类似

      { provide: MAT_DATE_LOCALE, useValue: 'de' }
      

      除了上述情况外,它都有效。所以问题是默认的“解析”方法不遵守这个 MAT_DATE_LOCALE 设置。

      另见How to implement MD_DATE_FORMATS for datepicker?

      【讨论】:

      • 非常感谢您的解决方案。这也救了我。
      猜你喜欢
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      相关资源
      最近更新 更多