37

I'm using the new version of angular and angular material. I need to get the value of the datepicker at the moment the user change the date to then pass that value to a function and do something.

datepicker

  <mat-form-field>
    <input matInput [matDatepicker]="picker" placeholder="Choose a date" [(ngModel)]="roomsFilter.date">
    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
    <mat-datepicker #picker [(ngModel)]="roomsFilter.date" ngDefaultControl (selectedChanged)="onChange($event)"></mat-datepicker>
  </mat-form-field>

and this the function.

  public onChange(event: any, newDate: any): void {
    console.log(event.target.value);
    // this.getData(newDate);
  }
Daniel
  • 3,541
  • 3
  • 33
  • 46
Miguel Frias
  • 2,544
  • 8
  • 32
  • 53
  • This seems to be working for me, [check here](https://plnkr.co/edit/rx5ddns6dWM7CdmqnKWg?p=preview) – Pankaj Parkar Oct 26 '17 at 19:36
  • I fixed. The datepicker was missing the event for some reason that still don´t why. – Miguel Frias Oct 26 '17 at 19:49
  • I assume you just want the value, so you can use `console.log(event)` ? https://plnkr.co/edit/8c21pXDrx7OCq93Gppa0?p=preview – AT82 Oct 28 '17 at 09:14
  • Don't you already have the value in the ngModel binding for the input field? I don't think you need the ngModel for the #picker itself. (btw - I know this is an old post, but just looking for testing my datePicker in jasmine test and ran across this.) – djmarquette Dec 21 '18 at 14:40

3 Answers3

38
<mat-form-field>
  <input matInput [matDatepicker]="expiration1" placeholder="Expiration" [formControl]="expiration" required (dateChange)="EndDateChange($event)">
  <mat-datepicker-toggle matSuffix [for]="expiration1"></mat-datepicker-toggle>
  <mat-datepicker #expiration1></mat-datepicker>
</mat-form-field>

Please check this demo link So you will get more idea. Example

Mikinj Mistry
  • 566
  • 1
  • 4
  • 16
17

Sorry I didn't post the answer before, but I solved the problem with the @AJT_82's comment. Here is the code:

Component HTML

  <mat-form-field>
    <input matInput [matDatepicker]="picker" placeholder="Choose a date" [(ngModel)]="roomsFilter.date">
    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
    <mat-datepicker #picker [(ngModel)]="roomsFilter.date" ngDefaultControl (selectedChanged)="onDate($event)"></mat-datepicker>
  </mat-form-field>

compoment ts

  public onDate(event): void {
    this.roomsFilter.date = event;
    this.getData(this.roomsFilter.date);
  }

Basically, I just passed the $event of the datepicker to get the value.

CJ Dennis
  • 4,226
  • 2
  • 40
  • 69
Miguel Frias
  • 2,544
  • 8
  • 32
  • 53
17

According to the official documentation, the MatDatepickerInput has a dateInput EventEmitter and it emits the selected date.

<mat-form-field>
 <input (dateInput)="OnDateChange($event.value)" matInput [matDatepicker]="picker" 
 [placeholder]="field.label" />
 <mat-datepicker-toggle matSuffix [for]="picker"> </mat-datepicker-toggle>
 <mat-datepicker #picker></mat-datepicker>
</mat-form-field>
De Bonheur
  • 742
  • 10
  • 13