I'm trying to set today as the max date in date input. But every time i choose today in the date picker, the model value was always undefined. Has someone met the same problem like this?
html code:
<form name="myForm" class="item">
<input type="date" ng-model="date" max="{{maxDateStr}}" name="dateInput">
<p> date: {{date | date:'yyyy-MM-dd'}}</p>
<p>myForm.$valid : {{myForm.$valid}}</p>
<p>myForm.dateInput.$error.max : {{myForm.dateInput.$error.max}}</p>
</form>
js code:
angular.module('angularApp',[])
.controller('MyCtrl', function($scope, $filter) {
$scope.maxDateStr = $filter('date')(new Date(), 'yyyy-MM-dd');
$scope.date = new Date();
});
my code on codepen
after debug in chrome, i found that when choose the max date, the value pass to NgModelController.$parsers had time e.g. Thu Jul 02 2015 16:54:00 GMT+0800 (CST). When run the max validator it returns false,
angular max validator code:
var maxVal;
ctrl.$validators.max = function(value) {
return !isValidDate(value) || isUndefined(maxVal) || parseDate(value) <= maxVal;
};
because the maxVal value was Thu Jul 02 2015 00:00:00, it returns false.