Set default value of HTML5 date input field with angularJS


I am hoping to bind the default value I generated in moment to the date and time input field. I tried ng-model and directly binding it to the value attributes. But none of it seems to work. Is there a way to make it work?

Edit: Also, how to bind the time input field as well?

<body ng-app="myApp">
<div ng-controller="MyCtrl">
    <input type="date" ng-model="date" value="{{date}}">
    <input type="time" ng-model="time" value="{{time}}">    

Here is a fiddle for it:

Problem courtesy of: Chris Yeung


If you're using chrome you have to specify the Date format as 'yyyy-MM-dd'.

$ = $filter("date")(, 'yyyy-MM-dd');

It simply won't work otherwise. Here's a working version

Solution courtesy of: Jeffrey W.


Working fiddle

{{date | date:'MM/dd/yyyy'}}

I changed the it to date and add date filter.

Discussion courtesy of: dpkk87

causes init error: The specified value "{{datum_default}}" does not conform to the required format, "yyyy-MM-dd".

solution: do not set date in the template!

<input type="date" ng-model="date_code">

assign the date in controller!

$scope.date_code = new Date();
Discussion courtesy of: NoblesCZ

This recipe can be found in it's original form on Stack Over Flow.