1

我使用Angular UI Bootstrap并创建一个包含两个日期选择器的表单。首先是今天的日期或我们可以选择的日期,第二天是第一天加上一周7天。Angular UI Bootstrap Datepicker动态日期(变量)

我能够让他们最初正确。

$scope.first = new Date(); $scope.second = new Date().setDate(new Date().getDate() + 7);

但是,如果我改变了第一日期选择的日期,第二届一个不会做任何改变。我发现{{dt | date:'fullDate' }}捕获动态日期。我的问题是我如何获得第一个datepickers的日期是一个变量,并在我的.js文件中操作?那么只要第一个日期发生变化,我就可以为我的第二个日期选择器添加+7天。

Sample code here

回答

1

如果你正在寻找与2个日期选择器。如果你在很多地方(页面)有相同的情况,你可以把它作为指令。

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']); 
 
angular.module('ui.bootstrap.demo').controller('DatepickerDemoCtrl', function ($scope) { 
 
    $scope.today = function() 
 
    { 
 
    $scope.dt = new Date(); 
 
    }; 
 
    $scope.today(); 
 

 
    $scope.clear = function() 
 
    { 
 
    $scope.dt = null; 
 
    }; 
 

 
    $scope.dateOptions = 
 
    { 
 

 
    formatYear: 'yy', 
 
    maxDate: new Date(2020, 5, 22), 
 
    minDate: new Date(), 
 
    startingDay: 1 
 
    }; 
 

 
    $scope.open1 = function() 
 
    { 
 
    $scope.popup1.opened = true; 
 
    }; 
 
    $scope.popup2 = {}; 
 

 
    $scope.open2 = function() 
 
    { 
 
    $scope.popup2.opened = true; 
 
    }; 
 
    
 
    $scope.popup1 = { 
 
    opened: false 
 
    }; 
 

 
    
 
    $scope.$watch('dt',function(val,old) 
 
    { 
 
     $scope.opened = false; 
 
     $scope.secondDate = new Date().setDate(new Date(val).getDate() + 7); 
 

 
    }); 
 
    
 
    console.log($scope.dt); 
 

 
});
<!doctype html> 
 
<html ng-app="ui.bootstrap.demo"> 
 
    <head> 
 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script> 
 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script> 
 
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.2.5.js"></script> 
 
    <script src="example.js"></script> 
 
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"> 
 
    </head> 
 
    <body> 
 

 
<style> 
 
    .full button span { 
 
    background-color: limegreen; 
 
    border-radius: 32px; 
 
    color: black; 
 
    } 
 
    .partially button span { 
 
    background-color: orange; 
 
    border-radius: 32px; 
 
    color: black; 
 
    } 
 
</style> 
 
<div ng-controller="DatepickerDemoCtrl"> 
 
    <pre>Selected date is: <em>{{dt | date:'fullDate' }}</em></pre> 
 

 

 
    <h4>Popup</h4> 
 
    <div class="row"> 
 
     <div class="col-md-6"> 
 
     <p class="input-group"> 
 
      <input type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="dt" is-open="popup1.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" alt-input-formats="altInputFormats" /> 
 
      <span class="input-group-btn"> 
 
      <button type="button" class="btn btn-default" ng-click="open1()"><i class="glyphicon glyphicon-calendar"></i></button> 
 
      </span> 
 
     </p> 
 
     </div> 
 
<hr> 
 
     <div class="col-md-6"> 
 
     <p class="input-group"> 
 
      <input type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="secondDate" is-open="popup2.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" alt-input-formats="altInputFormats" /> 
 
      <span class="input-group-btn"> 
 
      <button type="button" class="btn btn-default" ng-click="open2()"><i class="glyphicon glyphicon-calendar"></i></button> 
 
      </span> 
 
     </p> 
 
     </div> 
 

 
    </div> 
 

 
    
 
</div> 
 
    </body> 
 
</html>

3

快速的解决方案。在第一次约会时使用观察者。无论何时第一次更改日期都会在第二天添加7天。

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']); 
 
angular.module('ui.bootstrap.demo').controller('DatepickerDemoCtrl', function ($scope) { 
 
    $scope.today = function() { 
 
    $scope.dt = new Date(); 
 
    }; 
 
    $scope.today(); 
 

 
    $scope.clear = function() { 
 
    $scope.dt = null; 
 
    }; 
 

 
    $scope.dateOptions = { 
 

 
    formatYear: 'yy', 
 
    maxDate: new Date(2020, 5, 22), 
 
    minDate: new Date(), 
 
    startingDay: 1 
 
    }; 
 

 
    $scope.open1 = function() { 
 
    $scope.popup1.opened = true; 
 
    }; 
 

 

 
    $scope.popup1 = { 
 
    opened: false 
 
    }; 
 
console.log($scope.dt); 
 

 
$scope.secondDate = new Date().setDate(new Date().getDate()+7) 
 
$scope.$watch('dt', function(newVal, oldVal){ 
 
    var date = $scope.dt 
 
    $scope.secondDate = new Date(date).setDate(new Date(date).getDate()+7) 
 
}) 
 
});
<!doctype html> 
 
<html ng-app="ui.bootstrap.demo"> 
 
    <head> 
 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script> 
 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script> 
 
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.2.5.js"></script> 
 
    <script src="example.js"></script> 
 
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"> 
 
    </head> 
 
    <body> 
 

 
<style> 
 
    .full button span { 
 
    background-color: limegreen; 
 
    border-radius: 32px; 
 
    color: black; 
 
    } 
 
    .partially button span { 
 
    background-color: orange; 
 
    border-radius: 32px; 
 
    color: black; 
 
    } 
 
</style> 
 
<div ng-controller="DatepickerDemoCtrl"> 
 
    <pre>Selected date is: <em>{{dt | date:'fullDate' }}</em></pre> 
 
    <pre>Second date is: <em>{{secondDate | date:'fullDate' }}</em></pre> 
 

 

 
    <h4>Popup</h4> 
 
    <div class="row"> 
 
     <div class="col-md-6"> 
 
     <p class="input-group"> 
 
      <input type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="dt" is-open="popup1.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" alt-input-formats="altInputFormats" /> 
 
      <span class="input-group-btn"> 
 
      <button type="button" class="btn btn-default" ng-click="open1()"><i class="glyphicon glyphicon-calendar"></i></button> 
 
      </span> 
 
     </p> 
 
     </div> 
 

 
     
 

 
    </div> 
 

 
    
 
</div> 
 
    </body> 
 
</html>