Bind an input with type datetime-local to a Date property in Angular 2

前端 未结 4 1284
太阳男子
太阳男子 2020-12-05 18:13

It is possible to bind a component property of Date type to a HTML5 input with type set to datetime-local?

In my component I have a poperty:

<         


        
4条回答
  •  囚心锁ツ
    2020-12-05 18:48

    Inspired by @ne1410s answer I ended doing something very similar but without losing the date type.

    I used a pipe to declare the ngModel and call a method dateChanged just to return the conversion of the new Date in the ts.

    html code:

    
    

    ts code:

     dateChanged(eventDate: string): Date | null {
       return !!eventDate ? new Date(eventDate) : null;
     }
    

提交回复
热议问题