繁体   English   中英

Angular ui.bootstrap datepicker 动态最小日期不起作用

[英]Angular ui.bootstrap datepicker dynamic min-date not working

我使用 Angular ui.bootstrap datepicker 我需要根据我选择的其他日期动态设置最小日期。 贝娄我添加了我尝试过的代码。 我将首先选择“到”日期。 根据选择的“到”日期,我需要为“从”日期设置最小日期。 'From' date min 应该是在 'To' Date 中选择的日期之前的 30 天。

JS。

$scope.showButtonBar = false;
$scope.disabled = true;
$scope.today = function() {
  $scope.dt;
};
$scope.dtmax = new Date();
$scope.dateformat = "dd/MM/yyyy";
$scope.today();
$scope.showto = function($event) {
  $scope.showdpto = true;
};
$scope.showfrom = function($event) {
  $scope.showdpfrom = true;
};

$scope.setFrom = function(){
  $scope.disabled = false;
  $scope.dfmax = $scope.dt;
  $scope.dfMin = $scope.dt-30; // this is not working
  //$scope.dfMin = ($scope.dt.getDate() - 30); //this is also not working

  $scope.df = "";
  $scope.$apply();
}
$scope.Search = function(){
    console.log($scope.dt);
    console.log($scope.df);
    console.log($scope.dfMax);
  }

HTML

  From:
  <input type="text" uib-datepicker-popup="{{dateformat}}" showWeeks="false"
         show-button-bar="false" ng-model="df" is-open="showdpfrom"
         max-date="dfmax" min-date="dfMin"
         ng-style="disabled ? {'background-color':'#000'}:{'background-color':'fff'}"/>
  <span>
    <button type="button" ng-disabled="disabled" class="btn btn-default"
            ng-click="showfrom($event)">
        <i class="glyphicon glyphicon-calendar"></i>
    </button>
  </span>
  TO:
  <input type="text" uib-datepicker-popup="{{dateformat}}" showWeeks="false"
         show-button-bar="false" 
         ng-model="dt" is-open="showdpto" max-date="dtmax"
         ng-change="setFrom()"/>
  <span>
<button type="button" class="btn btn-default" ng-click="showto($event)">
       <i class="glyphicon glyphicon-calendar"></i>
</button>
<button class="btn-confirm" ng-click="Search()">SEARCH</button>
</span>

min-date 属性接受值作为日期 object,因此您需要如下设置 dfMin 的值。

   $scope.dfMin = new Date($scope.dt);
   $scope.dfMin.setDate($scope.dt.getDate()-30);

$scope.dfMax 有效,因为 $scope.dt 已经是日期 object。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM