23
votes

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>
6

6 Answers

42
votes

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>
15
votes

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>
11
votes
<mat-form-field>
  <input matInput [matDatepicker]="picker" placeholder="Input & change events"
         (dateInput)="addEvent('input', $event)" (dateChange)="addEvent('change', $event)">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

And .ts

addEvent(type: string, event: MatDatepickerInputEvent<Date>) {
    //console.log(event.value)
}

Check here

5
votes

Use selectedChanged inside md-datepicker.

<md-datepicker #datePicker (selectedChanged)="updateCalcs($event)"></md-datepicker>

$event will emit the new value which you can use in your updateCalcs() function.

updateCalcs(event){
  console.log(event);
}

Similar demo

1
votes

I must add that (dateInput) wasn't always firing for me, ended up using (keyup)

code HTML

  <mat-form-field>
        <input #fromInput matInput [matDatepicker]="pickerFrom" placeholder="סנן תאריך התחלה" 
          (dateChange)="filterFrom($event)" (keyup)="filterInputKeyupFrom()">
        <mat-datepicker-toggle matSuffix [for]="pickerFrom"></mat-datepicker-toggle>
        <mat-datepicker #pickerFrom></mat-datepicker>
    </mat-form-field>

final code TS

 private parseInputDate(v):Date{
      //this was a manual change in the input and needs to be parsed
      let a:Array<any> = v.split('.')
      a.forEach((v, i) => a[i] = parseInt(v) + (i == 1 ? -1 : 0))
      console.log(new Date(a[2], a[1], a[0]))
      return  new Date(a[2], a[1], a[0])
  }

  //(dateInput) wasn't always firing for me, ended up using (keyup)
  filterInputKeyupFrom(){
    this.lastFrom = this.parseInputDate(this.fromInput.nativeElement.value)
    if (this.lastFrom) {
      this.lastFrom.setSeconds(-1)
    }
    this.filterDates()
  }

  filterInputKeyupUpTo(){
    this.lastTo = this.parseInputDate(this.toInput.nativeElement.value)
    if (this.lastTo) {
      this.lastTo.setDate(this.lastTo.getDate()+1)
    }
    this.filterDates()
  }

  filterFrom(e:MatDatepickerInputEvent<Date>){
    console.log('filterFrom')
    //changing manually the input and then exit from it fires here
    if(e.value){
      //need to clone the date or changes are applied on the Picker
      this.lastFrom = new Date(e.value)
      if (this.lastFrom) {
        this.lastFrom.setSeconds(-1)
      }
      this.filterDates()
    }
  }

  filterUpTo(e:MatDatepickerInputEvent<Date>){
    console.log('filterUpTo')
    if(e.value){
      this.lastTo = new Date(e.value)
      if (this.lastTo) {
        this.lastTo.setDate(this.lastTo.getDate()+1)
      }
      this.filterDates()
    }
  }
1
votes

you can use ng-change to get the changed Data

<md-datepicker ng-change="updateCalcs()"></md-datepicker>

Check Here