Angular 2 Date Input not binding to date value

后端 未结 10 1915
醉梦人生
醉梦人生 2021-01-30 16:16

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:

&l         


        
10条回答
  •  隐瞒了意图╮
    2021-01-30 16:29

    If you are using a modern browser there's a simple solution.

    First, attach a template variable to the input.

    
    

    Then pass the variable into your receiving method.

    
    

    In your controller just accept the parameter as type HTMLInputElement and use the method valueAsDate on the HTMLInputElement.

    submit(date: HTMLInputElement){
        console.log(date.valueAsDate);
    }
    

    You can then manipulate the date anyway you would a normal date.

    You can also set the value of your as you would normally.

    Personally, as someone trying to stay true to the unidirectional data flow, i try to stay away from two way data binding in my components.

提交回复
热议问题