Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular material Datepicker get value on change

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);   } 
like image 908
Miguel Frias Avatar asked Oct 26 '17 19:10

Miguel Frias


People also ask

How to use datepicker with angular material?

For input event, Angular Material provides (dateInput) event for Datepicker. The dateInput triggers when user enters date manually in input box as well as selecting date from calendar. The dateInput event is bound to Datepicker input text.

What is (dateinput) event in Angular Material?

For input event, Angular Material provides (dateInput) event for Datepicker. The dateInput triggers when user enters date manually in input box as well as selecting date from calendar.

How to get date and date range with custom date formats in angular?

Most of the Angular projects prefer the Material UI library due to its compatibility and ease of usage. We know by default the Material UI library only provides the Datepicker component using which we can get Date and Date range with custom date formats as well.

How to change date manually in datepicker using datechange event?

The (dateChange) is the change event to trigger when we change date manually in input box or select date from calendar. The dateChange is used in Datepicker input text as following. We have created handleDOBChange () method in our component. On date change, the handleDOBChange () will execute. Find its code.


2 Answers

<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

like image 181
Mikinj Mistry Avatar answered Sep 26 '22 11:09

Mikinj Mistry


According to the official documentation, the MatDatepickerInput has a dateInput EventEmitter and it's 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> 
like image 42
De Bonheur Avatar answered Sep 22 '22 11:09

De Bonheur