Why ngModelChange is not working properly?

468 Views Asked by At

I'm creating a form where the user selects a date in an input, then the input 'week' automatically calculates the week of the year of respective date. I'm doing this:

[ngModel]="maintenance.date |  date:'w'"  (ngModelChange)="maintenance.week = $event.target.value"

the output: maintenance.week = 0

maintenance.date has the date selected in previous date input. but, as you can see, this doesn't work.

What does works is if I change (ngModelChange) for (mouseenter) but it's not the best way because the user has to pass the mouse over the input 'week' element in order for this to work:

[ngModel]="maintenance.date |  date:'w'"  (mouseenter)="maintenance.week = $event.target.value"

output: maintenance.week = 48 <- this is the correct week for date: 11/24/2020

Hope you can help me.

1

There are 1 best solutions below

0
On

(ngModelChange) will emit a value of the ngModel that is if the value of the ngModel is 01-01-01 then the $event value will be 01-01-01

Therefore to solve the problem change your code to (ngModelChange)="maintenance.week = $event"