I am using Angular 1.6 and I would like to directly bind a string to an input of type date instead of having to convert the date to a Date and then bind to the input. The reason is that I get JSON (along with other data) from the server and I don't want to create an intermediary variable only for the date, I want to use directly the JSON, and thus I can POST back my JSON as is when there are modifications in input field and no need to use ng-change and convert date and put it my JSON, etc... I have plunkered my issue.
Here is the html :
<body ng-app="plunker" ng-controller="MainCtrl">
<form name="myForm">
<label for="exampleInput">Date input</label>
<input type="date" id="exampleInput" name="input" ng-model="date" placeholder="yyyy-MM-dd"/>
</form>
</body>
And here is the javascript :
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.dateString = '2017-01-31';
$scope.date = new Date(2017, 11, 31);
});
If I bind the input to variable $scope.date it is OK but it is KO if I bind it to variable $scope.dateString.
Dateobject, then, if you want to display that date, you can just apply a filter on it{{ date | date }}. You can instantiate you Date object into the service, just when you receive the json.ngModelControllerfunctions and make custom parser and viewer docs.angularjs.org/api/ng/type/…