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

前端 未结 4 1273
太阳男子
太阳男子 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:57

    Now that its Spring 2017, DatePipe is shipped OOTB. You can achieve (one-way) binding by specifying format parameters to the pipe. For example:

    
    

    Slight caveat is that you can not use two-way binding with this technique, you have to use one way binding with the data pipe, then manage the DOM to model change events to handle client changes to the control (unless I'm missing something!), but it seems a lot cleaner this way.


    Update

    Looks like I was indeed missing something!

    Adding ngModelChange to the above should provide the DOM --> model side of the two-way binding process:

    
    

提交回复
热议问题