设定分钟日期在angularJS输入当前日期 [英] Set min date to current date in angularJS input
问题描述
在AngularJS,我们怎么能设置分
INPUT TYPE =日期
为当前日期的属性(今天的)?
<输入类型=日期NG模型=data.StartDateNAME =起始日期分=? />
EDIT1
我做了什么是以下建议并添加这控制器 -
$ scope.currentDate =新的日期();
这在HTML中 -
<输入类型=日期NG模型=data.StartDateNAME =起始日期需要分={{的currentdate |日期:'YYYY-MM-DD '}}/>
<跨度NG秀=$ form.StartDate脏放大器;&安培; form.StartDate $无效>
<跨度NG秀=$ form.StartDate error.required。>的开始日期和LT; / SPAN>
<跨度NG秀=$ form.StartDate error.min。>开始日期应不小于当前日期和LT; / SPAN>
< / SPAN>
现在,只不允许年内选择低于2015年。此外,如果整个日期小于当前日期,则没有验证正在发生。需要验证工作正常。为 .min
来检查现场正确的方式?
感谢
是的,你可以设置一个最小值。据文档:
<输入类型=日期
NG-模式=
[NAME =]
[分钟=]
[最大值=]
[需要=]
[NG-要求=]
[纳克变=]≥
参数
分 (选件)的字符串的 - 如果设置的值最小验证错误的关键
进入小于分钟。这必须是一个有效的ISO日期字符串
(YYYY-MM-DD)。
块引用>修改
要将字段设置为当前日期:控制器:
功能Ctrl键($范围)
{
$ scope.date =新的日期();
}查看:
<输入类型=日期NG模型=data.StartDate
NAME =起始日期分钟={{日期|日期:'YYYY-MM-DD'}}/>工作例如这里。
In AngularJS, how can we set the
min
attribute ofinput type="date"
to current date (today's) ?<input type="date" ng-model="data.StartDate" name="StartDate" min=?? />
Edit1
I did what was suggested below and added this in controller -
$scope.currentDate = new Date();
And this in Html -
<input type="date" ng-model="data.StartDate" name="StartDate" required min="{{currentDate | date:'yyyy-MM-dd'}}" /> <span ng-show="form.StartDate.$dirty && form.StartDate.$invalid"> <span ng-show="form.StartDate.$error.required">Start Date is required</span> <span ng-show="form.StartDate.$error.min">Start Date should not be less than current date</span> </span>
Now, only the year is not allowing to be selected less than 2015. Also, if whole date is less than current date, then no validation is occuring. Required validation is working fine. Is
.min
correct way to check the field?Thanks
解决方案Yes you can set a minimum value. According to docs:
<input type="date" ng-model="" [name=""] [min=""] [max=""] [required=""] [ng-required=""] [ng-change=""]>
Arguments
min (optional) string - Sets the min validation error key if the value entered is less than min. This must be a valid ISO date string (yyyy-MM-dd).
EDIT To set the field to current date:
controller:
function Ctrl($scope) { $scope.date = new Date(); }
view:
<input type="date" ng-model="data.StartDate" name="StartDate" min="{{date | date:'yyyy-MM-dd'}}" />
Working example here.
这篇关于设定分钟日期在angularJS输入当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!