加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在angularJS输入中将最小日期设置为当前日期

发布时间:2020-12-17 08:01:21 所属栏目:安全 来源:网络整理
导读:在AngularJS中,我们如何将input type =“date”的min属性设置为当前日期(今天)? input type="date" ng-model="data.StartDate" name="StartDate" min=?? / EDIT1 我做了下面的建议并在控制器中添加了这个 – $scope.currentDate = new Date(); 这在Html中
在AngularJS中,我们如何将input type =“date”的min属性设置为当前日期(今天)?
<input type="date" ng-model="data.StartDate" name="StartDate" min=?? />

EDIT1

我做了下面的建议并在控制器中添加了这个 –

$scope.currentDate = new Date();

这在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>

现在,只有年份不允许选择少于2015年.此外,如果整个日期小于当前日期,则不会进行验证.所需的验证工作正常.是.min正确检查现场的方法吗?

谢谢

是的,您可以设置最小值.根据 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).

编辑
要将字段设置为当前日期:

控制器:

function Ctrl($scope)
{
    $scope.date = new Date();
}

视图:

<input type="date" ng-model="data.StartDate" 
        name="StartDate" min="{{date | date:'yyyy-MM-dd'}}" />

工作示例here.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读