Angular 材质日期选择器 (MatDatepicker) 自动完成焦点移出时的日期

Jus*_*now 5 datepicker angular-material angular angular6 angular-material-datetimepicker

我在我的项目中使用角度材料/垫日期选择器,每当用户在其中输入一位数字并失去焦点时,日期选择器就会自动完成随机日期。

例子

只需进入1并离开焦点,或者进入5/5/20

日期选择器自动添加日期

更新:我还使用反应式表单,并使用该方法验证输入

我可以禁用这个吗?

小智 1

我知道现在回答这个问题已经太晚了。但是,我也遇到了与反应式表单相同的问题,并通过以下方式禁用 MatDatePicker 自动完成功能。

<mat-form-field>
   <input matInput #autocomplete placeholder="Choose a date" formControlName="datePicker">
   <input type="hidden" [matDatepicker]="picker" placeholder="Choose a date" formControlName="datePicker" (dateChange)="autocomplete.value = toFormattedDate($event.value)">
   <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
   <mat-datepicker #picker></mat-datepicker>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)

在组件中创建以下“toFormattedDate”方法。

toFormattedDate(iso: string) {
  const date = new Date(iso);
  console.log(date);
  return `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`;
}
Run Code Online (Sandbox Code Playgroud)