Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Date Input not binding to date value

trying to get a form set up but for some reason, the Date input in my html is not binding to the object's date value, despite using [(ngModel)]

html:

<input type='date' #myDate [(ngModel)]='demoUser.date'/><br> 

form component:

export class FormComponent {     demoUser = new User(0, '', '', '', '', new Date(), '', 0, [], []);   } 

User class:

export class User {     constructor (         public id: number,         public email: string,         public password: string,         public firstName: string,         public lastName: string,         public date: Date,         public gender: string,         public weight: number,         public dietRestrictions: string[],         public fitnessGoals: string[]     ){      } } 

A test output reveals the current "new" Date as the object's value, but the input doesn't update the User object's date value or reflect the value, suggesting neither of the two-way bindings are working. Help would be greatly appreciated.

like image 316
MoSheikh Avatar asked Jul 03 '16 23:07

MoSheikh


People also ask

How do I change the input date format?

To set and get the input type date in dd-mm-yyyy format we will use <input> type attribute. The <input> type attribute is used to define a date picker or control field. In this attribute, you can set the range from which day-month-year to which day-month-year date can be selected from.


1 Answers

Angular 2 , 4 and 5 :

the simplest way : plunker

<input type="date" [ngModel] ="dt | date:'yyyy-MM-dd'" (ngModelChange)="dt = $event"> 
like image 113
El houcine bougarfaoui Avatar answered Oct 08 '22 02:10

El houcine bougarfaoui