【问题标题】:Angular datepicker setDate角度日期选择器 setDate
【发布时间】:2020-11-13 10:14:58
【问题描述】:

我有一个带有数据选择器的屏幕。当我已经有一个数据参数时,它会在我的日历中选择我 2 个日期:日期参数加上当天的日期。如果我有一个日期作为参数怎么办,以便它选择我作为日期作为参数而不是日期

    <mat-label>Select a date</mat-label>
        <input matInput [matDatepicker]="picker"
        [(ngModel)]="myDate"
        (dateChange)="onChange($event)"
        placeholder="">
        <mat-datepicker #picker ></mat-datepicker>
        <mat-datepicker-toggle matSuffix [for]="picker" >

        </mat-datepicker-toggle>

【问题讨论】:

  • 只需给变量“myDate”赋值,记住这是一个javaScript的object Date,你的数据参数可能是一个字符串,在这种情况下,你需要make类似myDate=new Date(myparameter)
  • 当我到达我的表格时,我的日期是准确的。所以它选择我 myDate 和日期
  • ::glups:: 对不起,我不明白你的意思

标签: angular date datepicker


【解决方案1】:

我不喜欢使用 ::ng-deep,所以我的方法是在 style.css 中添加一些类似的(适用于所有应用程序)

.noTodayDatepicker .mat-calendar-body-today:not(.mat-calendar-body-selected){
    border-color:transparent!important;
}
.noTodayDatepicker .mat-calendar-body-today:not(:hover):not(.mat-calendar-body-selected){
  background-color:transparent!important;
}
.noTodayDatepicker .mat-calendar-body-today.mat-calendar-body-selected {
    box-shadow: none;
}

那么就可以使用pannelClass了

  <mat-datepicker #picker panelClass="noTodayDatepicker"></mat-datepicker>

stackblitz

【讨论】:

    【解决方案2】:

    这类似于这个问题和答案: How to autofocus with highlight blue color on today date in datepicker of angular material?

    Angular Material 会将样式应用于今天的日期,这并不意味着选择了日期,它只是由于其 CSS 而以某种方式显示

    您可以在组件的 CSS 中定位选定的 Today 节点,方法是添加此节点并将其更改为您需要的样子。

    ::ng-deep.mat-calendar-body-today:not(.mat-calendar-body-selected) {
      background-color: white;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-31
      相关资源
      最近更新 更多