手动选择输入日期到最大返回未定义

时间:2018-10-25 12:57:04

标签: javascript html angularjs date datepicker

我有两个设置了最大和最小的日期选择器。如果将起始日期最大值设置为特定日期,而我手动选择该日期,它将返回无效日期。选择任何其他日期都将返回正确的值。(例如,如果最大设置为10月24日,而我手动选择了该日期,那么它将返回未定义的值)

 <div class="col-sm-2">
  <h6>From Date</h6>
    <input type="date" onkeydown="return false" id="fromDate" class="form-control" ng-change="fdateSel()" max="{{fmaxdate}}" ng-model="fromDate">    
 </div>
 <div class="col-sm-2">
   <h6>To Date</h6>
   <input type="date" onkeydown="return false" id="toDate" class="form-control" ng-change="tdateSel()" min="{{tmindate}}" max="{{tmaxdate}}" ng-model="toDate">
 </div>

这是控制者

$scope.fromDate = new Date((new Date()).valueOf() - 1000 * 3600 * 24 * 7);
$scope.toDate = new Date();

$scope.fmaxdate = $filter('date')(new Date($scope.toDate.valueOf() - 1000 * 3600 * 24), "yyyy-MM-dd");
$scope.tmindate = $filter('date')(new Date($scope.fromDate.valueOf() + 1000 * 3600 * 24), "yyyy-MM-dd");
$scope.tmaxdate = $filter('date')(new Date(), "yyyy-MM-dd");

$scope.fdateSel = function () {
        $scope.tmindate = $filter('date')(new Date($scope.fromDate.valueOf() + 1000 * 3600 * 24), "yyyy-MM-dd");
    }

$scope.tdateSel = function () {
        if (undefined != $scope.toDate)
        $scope.fmaxdate = $filter('date')(new Date($scope.toDate.valueOf() - 1000 * 3600 * 24), "yyyy-MM-dd");
    }

1 个答案:

答案 0 :(得分:0)

我无法复制您看到的错误,但我确实注意到您在输入中缺少ng-model属性。让我知道是否有帮助。

下面是一个有效的示例:

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

app.controller('exampleController', ['$scope', function($scope) {
  $scope.fromDate = new Date((new Date()).valueOf() - 1000 * 3600 * 24 * 7);
  $scope.toDate = new Date();

  $scope.fmaxdate = new Date($scope.toDate.valueOf() - 1000 * 3600 * 24);
  $scope.tmindate = new Date($scope.fromDate.valueOf() + 1000 * 3600 * 24);
  $scope.tmaxdate = new Date();

  $scope.fdateSel = function () {
         if (undefined != $scope.fromDate) {
          $scope.tmindate = new Date($scope.fromDate.valueOf() + 1000 * 3600 * 24);
         }
      }

  $scope.tdateSel = function () {
          if (undefined != $scope.toDate) {
            $scope.fmaxdate = new Date($scope.toDate.valueOf() - 1000 * 3600 * 24);
            }
      }
  }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>

<div ng-app="app" ng-controller="exampleController as ctrl">
  <div class="col-sm-2">
    <h6>From Date</h6>
    <input type="date" onkeydown="return false" ng-model="fromDate" id="fromDate" class="form-control" ng-change="fdateSel()" max="{{fmaxdate}}">    
  </div>
  <div class="col-sm-2">
   <h6>To Date</h6>
   <input type="date" onkeydown="return false" ng-model="toDate" id="toDate" class="form-control" ng-change="tdateSel()" min="{{tmindate}}" max="{{tmaxdate}}">
 </div>
</div>