在焦點上開啟 datepicker

此示例還包括使用屬性:

  • 最大
  • startAt
  • startView
  • 頭錘

日期選擇器 - 概述 - example.html 的:

<h2>Options</h2>
<p>
  <md-checkbox [(ngModel)]="touch">Use touch UI</md-checkbox>
  <md-checkbox [(ngModel)]="filterOdd">Filter odd months and dates</md-checkbox>
  <md-checkbox [(ngModel)]="yearView">Start in year view</md-checkbox>
</p>
<p>
  <md-input-container>
    <input mdInput [mdDatepicker]="minDatePicker" [(ngModel)]="minDate" placeholder="Min date" (keydown)="false" (click)="minDatePicker.open()">
    <button mdSuffix [mdDatepickerToggle]="minDatePicker"></button>
  </md-input-container>
  <md-datepicker #minDatePicker [touchUi]="touch"></md-datepicker>
  <md-input-container>
    <input mdInput [mdDatepicker]="maxDatePicker" [(ngModel)]="maxDate" placeholder="Max date" (keydown)="false" (focus)="maxDatePicker.open()">
    <button mdSuffix [mdDatepickerToggle]="maxDatePicker"></button>
  </md-input-container>
  <md-datepicker #maxDatePicker [touchUi]="touch"></md-datepicker>
</p>
<p>
  <md-input-container>
    <input mdInput [mdDatepicker]="startAtPicker" [(ngModel)]="startAt" placeholder="Start at date" (keydown)="false" (focus)="startAtPicker.open()">
    <button mdSuffix [mdDatepickerToggle]="startAtPicker"></button>
  </md-input-container>
  <md-datepicker #startAtPicker [touchUi]="touch"></md-datepicker>
</p>

<h2>Result</h2>

<p>
  <button [mdDatepickerToggle]="resultPicker"></button>
  <md-input-container>
    <input mdInput
           #resultPickerModel="ngModel"
           [mdDatepicker]="resultPicker"
           [(ngModel)]="date"
           [min]="minDate"
           [max]="maxDate"
           [mdDatepickerFilter]="filterOdd ? dateFilter : null"
           placeholder="Pick a date"
           (keydown)="false"
           (focus)="resultPicker.open()">
    <md-error *ngIf="resultPickerModel.hasError('mdDatepickerMin')">Too early!</md-error>
    <md-error *ngIf="resultPickerModel.hasError('mdDatepickerMax')">Too late!</md-error>
    <md-error *ngIf="resultPickerModel.hasError('mdDatepickerFilter')">Date unavailable!</md-error>
  </md-input-container>
  <md-datepicker
      #resultPicker
      [touchUi]="touch"
      [startAt]="startAt"
      [startView]="yearView ? 'year' : 'month'">
  </md-datepicker>
</p>
<p>
  <input [mdDatepicker]="resultPicker2"
         [(ngModel)]="date"
         [min]="minDate"
         [max]="maxDate"
         [mdDatepickerFilter]="filterOdd ? dateFilter : null"
         (focus)="resultPicker2.open()"
         placeholder="Pick a date"
         (keydown)="false">
  <button [mdDatepickerToggle]="resultPicker2"></button>
  <md-datepicker
      #resultPicker2
      [touchUi]="touch"
      [startAt]="startAt"
      [startView]="yearView ? 'year' : 'month'">
  </md-datepicker>
</p>

日期選擇器 - 概述 - example.ts:

import {Component, OnInit} from '@angular/core';
import {MdDatepicker} from '@angular/material';

@Component({
  selector: 'datepicker-overview-example',
  templateUrl: 'datepicker-overview-example.html'
})
export class DatepickerOverviewExample implements OnInit {
  
  touch: boolean;
  filterOdd: boolean;
  yearView: boolean;
  minDate: Date;
  maxDate: Date;
  startAt: Date;
  date: Date;
  dateFilter = (date: Date) => date.getMonth() % 2 == 1 && date.getDate() % 2 == 0;
  
}

現場演示