繁体   English   中英

如何将昨天的日期作为出现在 mat-datepicker 中的日期 - angular 素材

[英]How to make yesterday's date as the date to appear in mat-datepicker - angular material

我想更新并使昨天的日期显示为 mat-datepicker 中的日期。

我尝试通过以下方式实现它,但没有成功。 我的代码 -

app.component.html

<mat-form-field class="example-full-width" [floatLabel]="hideLabel">
                <input matInput [formControl]="onlineDate" name="onlineDate" [max]="currentDate"  
                  [matDatepicker]="picker" required placeholder="Online Date "
                  autocomplete="off" />
                <mat-datepicker-toggle matSuffix [for]="picker">
                </mat-datepicker-toggle>
                <mat-datepicker #picker></mat-datepicker>
                
              </mat-form-field> 

app.component.ts

export class ApplicantComponent implements OnInit, AfterViewInit {
  onlineDate: FormControl;
  currentDate = new Date();
  today = new Date()
  yesterday = new Date();


 constructor(
       this.onlineDate = new FormControl(this.yesterday.setDate(this.yesterday.getDate() - 1))
  }

}

使用上面的代码,我无法显示任何日期。 截图供参考-

在此处输入图像描述

MatDatePicker 采用一个@Input() value修饰属性。 它不在模板的日期选择器中。

尝试添加这个:

<input matInput [value]="onlineDate" 
    [formControl]="onlineDate" name="onlineDate" [max]="currentDate"  
    [matDatepicker]="picker" required placeholder="Online Date "autocomplete="off" />

我通过放置解决了我的问题 -

 constructor(
       this.onlineDate = new FormControl(new Date(new Date().setDate(new Date().getDate() + 1)));

  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM