Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在angularJS输入中将最小日期设置为当前日期_Angularjs - Fatal编程技术网

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

在angularJS输入中将最小日期设置为当前日期,angularjs,Angularjs,在AngularJS中,如何将input type=“date”的min属性设置为当前日期(今天的) 这是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.

在AngularJS中,如何将
input type=“date”
min
属性设置为当前日期(今天的)

这是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
检查字段的方法正确吗


谢谢

是的,您可以设置最小值。根据:

视图:



工作示例。

根据角度文档

min(可选)字符串设置值为 输入的值小于最小值。这必须是有效的ISO日期字符串 (yyyy-MM-dd)

您可以将其绑定到范围变量,或者直接将表达式放入属性中,它只需要采用正确的格式(javascript日期对象有一个toISOString()方法)。还应注意,这并不阻止用户选择低于最小值的日期,而是设置验证键以指示该字段无效

编辑:

脚本

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  var today=new Date();
  $scope.today = today.toISOString();
});
Html

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.14/angular.js" data-semver="1.3.14"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <p>Hello {{name}}!</p>
    <form name="myForm">
    <input type="date" ng-model="StartDate" name="StartDate" required min="{{today}}" />
    <span ng-show="myForm.StartDate.$dirty && myForm.StartDate.$invalid">
    <span ng-show="myForm.StartDate.$error.required">Start Date is required</span>
    <span ng-show="myForm.StartDate.$error.min">Start Date should not be less than current date</span>

</span>
</form>
  </body>

</html>

安古拉斯普朗克
文件。写(“”);
你好{{name}}

开始日期是必需的 开始日期不应小于当前日期
您的错误消息表明您希望日期不大于当前日期,如果是这种情况,只需更改即可

  <input type="date" ng-model="StartDate" name="StartDate" required min="{{today}}" />



只需添加控制器即可

$scope.today=new Date() // calculates current date
并在html代码中添加-

min-date="today"
var today=new Date().toISOString().split('T')[0];
document.getElementsByName(“appo_date”)[0].setAttribute('min',today)

正确答案已经在上面了, 如果有人想在2/4/5角上找到一个解决方案,这里有一个方法

在you.ts文件中

private todate = new Date();
在html文件中

 <input type="date" min="{{todate | date:'yyyy-MM-dd'}}" class="form-control" [(ngModel)]="datemodel" id="input-12">

此外,您还可以在此文件中提供其他格式选项


function(){return new Date();}
我想将它设置为当前日期。它对我有效,用一个例子更新了我的答案。是的,它是正确的。只是一个问题,你是如何填写表格的?您是否使用相同的ISO8601格式,例如:2015-03-10?虽然我绑定的数据是yyyy-MM-dd格式,但它是以dd-MM-yyy格式显示的。也许这就是问题所在,尽管我只是在Safari上测试了它(它不支持日期字段)。以dd-MM-yyy格式键入日期可以正常工作。在Chrome上有一个日期选择器,它将日期显示为dd-MM-yyyy,但是验证工作也很好。我有一个类似的问题!虽然my正在选择最小值和最大值。最大值和最小值是否仍可用于拾取?我的意思是,它们没有变灰,所以可以选择,但是当选择时仍然会抛出错误,这有点奇怪。是不是因为它与最小值和最大值进行了严格的比较,因此选择它们会导致错误,难道没有办法避免吗?我有一个类似的问题。按照上面提供的步骤,选择返回日期时会显示错误。但仍然允许在提交时创建记录。还有,今天之前的日期不可选择吗?除非你有“具体细节”的答案,否则尽量避免在答案中评论。请使用下面的评论框,一旦你有足够的声誉,你将能够对任何帖子发表评论。:)你能再核对一下我的答案吗?)(请注意:)
  <input type="date" ng-model="StartDate" name="StartDate" required min="{{today}}" />
  <input type="date" ng-model="StartDate" name="StartDate" required max="{{today}}" />
$scope.today=new Date() // calculates current date
min-date="today"
private todate = new Date();
 <input type="date" min="{{todate | date:'yyyy-MM-dd'}}" class="form-control" [(ngModel)]="datemodel" id="input-12">