Components: DatePicker not updating ngModel value

Created on 17 Apr 2018  路  5Comments  路  Source: angular/components

Bug?

I have this Date Picker example

<mat-form-field>
  <input matInput [(ngModel)]="from" [matDatepicker]="from" (change)="onChangeEvent($event)" placeholder="From">

  <mat-datepicker-toggle matSuffix [for]="from"></mat-datepicker-toggle>
  <mat-datepicker #from></mat-datepicker>
</mat-form-field>
import {Component} from '@angular/core';

@Component({
  selector: 'date-filter',
  templateUrl: 'date.filter.html',
})
export class DateFilter {
  from: any;

  onChangeEvent(e): void {
    console.log(e.target.value);
    console.log('this.from = ', this.from);
  }
}

What is the expected behavior?

On date change i should have the value in this.form.

What is the current behavior?

It doesn't update the model and i get this error: Uncaught Error: Cannot assign to a reference or variable!.

Most helpful comment

please use (dateChange)="onValueChange($event) instead of change

All 5 comments

please ask this question in StackOverflow

I just found the solution myself, thank you for your awesome support.

<mat-form-field>
  <input matInput [(ngModel)]="fromDate" [matDatepicker]="from" (change)="onChangeEvent($event)" placeholder="From">

  <mat-datepicker-toggle matSuffix [for]="from"></mat-datepicker-toggle>
  <mat-datepicker #from></mat-datepicker>
</mat-form-field>

I had to update the [(ngModel)] to [(ngModel)]="fromDate" differently from [matDatepicker]="from".

Completely unintuitive.

please use (dateChange)="onValueChange($event) instead of change

@dhrn it was just for debugging purposes

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alanpurple picture alanpurple  路  3Comments

jelbourn picture jelbourn  路  3Comments

Hiblton picture Hiblton  路  3Comments

crutchcorn picture crutchcorn  路  3Comments

constantinlucian picture constantinlucian  路  3Comments