angularjs - Strange error with date input in angular -
i'm trying set today max date in date input. every time choose today in date picker, model value undefined. has met same problem 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, found when choose max date, value pass ngmodelcontroller.$parsers had time e.g. thu jul 02 2015 16:54:00 gmt+0800 (cst). when run max validator returns false,
angular max validator code:
var maxval; ctrl.$validators.max = function(value) { return !isvaliddate(value) || isundefined(maxval) || parsedate(value) <= maxval; };
because maxval
value thu jul 02 2015 00:00:00
, returns false.
replace code following
angular.module('angularapp',[]) .controller('myctrl', function($scope, $filter) { $scope.maxdatestr = $filter('date')(new date(), 'yyyy-mm-dd'); $scope.date = new date(); });
with
angular.module('angularapp',[]) .controller('myctrl', function($scope, $filter) { $scope.maxdatestr = $filter('date')(new date(), 'yyyy-mm-dd'); $scope.date = new date((new date()).getfullyear(), (new date()).getmonth(), (new date()).getdate()); });
note: create date 00:00:00
time, should perfect working max validation.
Comments
Post a Comment