使用事件將選定的日期值傳遞給函式

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

<md-input-container>
    <input mdInput [mdDatepicker]="picker" placeholder="Choose a date" [(ngModel)]="value">
    <button mdSuffix [mdDatepickerToggle]="picker"></button>
</md-input-container>
<md-datepicker #picker [startAt]="startDate" (selectedChanged)="selectedDate($event)"></md-datepicker>

<p>ngModel Value: {{value}}</p>

<p>Date from selectedDate(): {{checkDate}}</p>

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

import {Component} from '@angular/core';

@Component({
  selector: 'datepicker-overview-example',
  templateUrl: 'datepicker-overview-example.html'
})
export class DatepickerOverviewExample {
  
  value: Date = new Date();
  
  checkDate: Date;
  
  selectedDate(date){
   // ngModel still returns the old value
      console.log("ngModel: " + this.value);
      
   // date passes the newly selected value  
      console.log("Selected Value: " + date);
      this.checkDate = date;
  }
}

現場演示