0

I have many views, each one related to js controller using angularjs. I have a purchases form where there is :

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

In the controller js I have :

$scope.currentDate=$filter('date') (new Date(), 'yyyy-MM-dd');

I used that method in all my forms and working fine but in the purchase form the date is not set as today and in the console the page gave me :

angular.js:12450Error: [ngModel:datefmt] http://errors.angularjs.org/1.4.6/ngModel/datefmt?p0=2017-01-24
    at angular.js:38
    at Array.<anonymous> (angular.js:21769)
    at Object.<anonymous> (angular.js:25349)
    at n.$digest (angular.js:15751)
    at n.$apply (angular.js:16030)
    at angular.js:1660
    at Object.e [as invoke] (angular.js:4476)
    at d (angular.js:1658)
    at yc (angular.js:1678)
    at Xd (angular.js:1572)

I didn't catch where is the problem. I checked the injector ( $filter), checked the ng-model if it's duplicate, nothing. Any idea?

5
  • 1
    If you check the error link (docs.angularjs.org/error/ngModel/datefmt?p0=2017-01-24), it actually gives you the reason for your error: Model is not a date object. So $scope.currentDate is a string, not a date --> an error is thrown Commented Jan 24, 2017 at 14:00
  • yes I checked the link, I defined a variable containing the date, where is the problem? Commented Jan 24, 2017 at 14:02
  • 2
    Your variable is not a date, it is a string. Just do $scope.currentDate = new Date() Commented Jan 24, 2017 at 14:02
  • 1
    Why not something like plnkr.co/edit/14zJVTa0oRvNWIG4lHOv?p=preview Commented Jan 24, 2017 at 14:02
  • Fissio, working, thank you so much Commented Jan 24, 2017 at 14:04

1 Answer 1

3

All date-related inputs like require the model to be a Date object. If the model is something else, this error will be thrown

This is explanation found in the error url provided.

$scope.currentDate = new Date();

This should work for you. You were providing the formatted date string as input, which is not expected.

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

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.