Elio Chamy
Elio Chamy

Reputation: 269

angularjs filter date, date format error

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?

Upvotes: 0

Views: 674

Answers (1)

Muthukannan Kanniappan
Muthukannan Kanniappan

Reputation: 2079

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.

Upvotes: 3

Related Questions