1

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.

4
  • the right way is to use the Date object, 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. Commented Aug 4, 2017 at 8:30
  • You can use ngModelController functions and make custom parser and viewer docs.angularjs.org/api/ng/type/… Commented Aug 4, 2017 at 8:30
  • @Hitmands : thanx but doesn't answer my need Commented Aug 4, 2017 at 9:05
  • @Icycool : not that simple but seems to be the best solution, thanx Commented Aug 4, 2017 at 9:05

2 Answers 2

2

You can do it by using value attribute of your input like this :

angular
  .module('plunker', [])
  .controller('MainCtrl', function($scope) {
    $scope.dateString = '2017-01-31';
    $scope.date = new Date(2017, 11, 31);
  })
;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<section ng-app="plunker" ng-controller="MainCtrl">
  
  <form name="myForm">
    <label for="exampleInput">Date input</label>
    
    <input 
      type="date" value="{{dateString | date : 'yyyy-MM-dd'}}" 
      ng-model="dateString" placeholder="yyyy-MM-dd"
    />
  </form>
  
</section>

Sign up to request clarification or add additional context in comments.

2 Comments

thanx, it seems to work but I am having error in the console : "Error: [ngModel:datefmt] errors.angularjs.org/1.5.11/ngModel/datefmt?p0=2017-01-31" and according anguar doc : AngularJS does not set validation errors on the <input> in this case as those errors are shown to the user
@JeanJacques $scope. dateString should not exist at all... {{ date | date: 'yyyy-MM-dd' }} instead
0

Placeholder attribute does not work with input type date. You can achieve this by changing your input type to text.

  <input type="text" onfocus="(this.type='date')" placeholder="{{some_date_holder}}"/>

Read this for more details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.