Problem with ngModel and input type = date

1

I have an angular an input like this:

 <input [ngModel]="pedido.fecha | date:'yyyy-MM-dd' : 'UTC'" placeholder="fecha" #fecha  type="date">

when loading the page, the input is correctly filled with the date that comes from the object pedido.fecha , however if I select a date by opening the datepicker the value pedido.fecha is still the original and not the one I have selected in the datepicker, this makes that when I update the data, I load the initial date, never the one I edited.

    
asked by Pedro 01.12.2018 в 04:44
source

1 answer

0

My problem is solved by using the $event object in conjunction with the event trigger (ngModelChange) .

<input [ngModel]="pedido.fecha | date:'yyyy-MM-dd' : 'UTC'"  (ngModelChange)="pedido.fecha = $event" placeholder="fecha" #fecha  type="date">

ngModelChange listens for updates of the model in the view, then when that happens I assign to the requested variable. It gives the value of $event which is the updated value after the change produced in the view and that solves my problem.

    
answered by 01.12.2018 / 07:02
source