Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material Datepicker: Change event not firing when selecting date

I'm using Angular and Angular Material's Datepicker. Everything is working fine for the most part, however I added a (change) event that is only working when the user manually types in a date. It does not get triggered when the user clicks on a date from the datepicker popup. To be clear, the value for date does in fact change when the user clicks on a date, it is just the (change) event, and ultimately my updateCalcs() function that for some reason doesn't get triggered. How can I trigger an event when the user clicks on a date from the datepicker?

<md-input-container>
    <input mdInput [mdDatepicker]="datePicker" placeholder="Choose Date" name="date" [(ngModel)]="date" (change)="updateCalcs()" required>
    <button mdSuffix [mdDatepickerToggle]="datePicker"></button>
</md-input-container>
<md-datepicker #datePicker></md-datepicker>
like image 710
Bryan Avatar asked Jul 14 '17 23:07

Bryan


People also ask

How can use datepicker in angular materials?

Add a template reference variable mat-datepicker element. Connect mat-datepicker to input element via [matDatepicker] property. Finally add date picker toggle button to display or hide calender popup by using mat-datepicker-toggle element.

Does angular material have time picker?

The Angular Time Picker, also known as the Angular Material Time Picker is a lightweight and mobile-friendly component that allows end users to select a time value either from a pop-up time list or by entering the value directly in the text box.

How can I get date from Mat datepicker?

Connecting a date-picker to an inputThe date-picker is made up of text input and a calendar pop-up, which is linked via the mat-Date-picker property to the text input. It also has an optional date-picker toggle button that gives the user a simple method to open the date-picker pop-up window.


2 Answers

There's a dateChange event that's raised both when the date is edited in the text box and when the date is changed via the calendar control. See here

<mat-form-field>
  <input matInput [matDatepicker]="datepicker" required placeholder="Choose a date" (dateChange)="valueChanged()">
  <mat-datepicker-toggle matSuffix [for]="datepicker"></mat-datepicker-toggle>
  <mat-datepicker #datepicker></mat-datepicker>
</mat-form-field>
like image 50
Stuart Hallows Avatar answered Oct 22 '22 06:10

Stuart Hallows


Replace change with ngModelChange

Change from

<input mdInput 
       [mdDatepicker]="datePicker" 
       placeholder="Choose Date" 
       name="date" [(ngModel)]="date" 
       (change)="updateCalcs()" required>

To

<input mdInput 
       [mdDatepicker]="datePicker" 
       placeholder="Choose Date" 
       name="date" [(ngModel)]="date" 
       (ngModelChange)="updateCalcs()" required>
like image 18
Long Field Avatar answered Oct 22 '22 06:10

Long Field