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>
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.
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.
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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With