網站搜尋

html - 在 Angular 12 中將輸入類型為日期的最小值設置為當前日期


我有一個日期選擇器輸入框,我想將它的最小值設置為當前日期

<input type="date" class="form-control" id="checkindate" placeholder="" min="">

我如何使用 Angular 做到這一點?

答案#1

有多種方法可以做到。min 屬性接受多種格式的字串。這是我使用 'yyyy-mm-dd' 的解決方案。

控制器 (*.ts)

export class AppComponent {
  currentDate: string;

  constructor() {
    // credit: https://stackoverflow.com/a/38148759/6513921
    this.currentDate = new Date().toISOString().slice(0, 10);
  }
}

範本 (*.html)

<input type="date" class="form-control" id="checkindate" placeholder="" [min]="currentDate">

工作範例:Stackblitz

答案#2

您需要將日期管道化以符合輸入格式。

這裡是示範