Angular Material Datepicker:选择日期时更改事件未触发

时间:2017-07-14 23:11:58

标签: angular angular-material

我正在使用Angular和Angular Material的Datepicker。在大多数情况下,一切都运行良好,但是我添加了date事件,该事件仅在用户手动输入日期时才起作用。当用户点击日期选择器弹出窗口中的日期时,它不会被触发。要明确的是,(change)的值实际上会在用户点击日期时发生变化,只是updateCalcs()事件,最终我的<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> 功能由于某种原因而无法发生变化被触发。当用户点击日期选择器中的日期时,如何触发事件?

var enabled = true;
displayResults(){
 if (enabled) {
  ...the rest of the code here
  enabled = false;
 }
}

6 个答案:

答案 0 :(得分:9)

change替换为ngModelChange

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

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

答案 1 :(得分:5)

SELECT G.*, M.* FROM locations L JOIN gems G ON L.locatable_id = G.id JOIN metals M ON L.locatable_id = M.id WHERE L.latitude BETWEEN 0.0000 AND 1.0000 AND L.longitude BETWEEN - 1.0000 AND - 2.0000 AND (L.locatable_type = 'Gem' OR L.locatable_type = 'Metal'); 内使用selectedChanged

md-datepicker

<md-datepicker #datePicker (selectedChanged)="updateCalcs($event)"></md-datepicker>会发出您可以在$event函数中使用的新值。

updateCalcs()

Similar demo

答案 2 :(得分:5)

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

和.ts

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

选中here

答案 3 :(得分:1)

您可以使用 ng-change 获取更改后的数据

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

检查Here

答案 4 :(得分:0)

我必须补充一点,(dateInput)并不总是为我解雇,最终使用了(keyup)

代码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>

最终代码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()
    }
  }

答案 5 :(得分:0)

在文本框中编辑日期和通过日历控件更改日期时,都会引发一个dateChange事件。 参见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>