我正在使用 Angular UI Bootstrap并创建一个包含两个日期选择器的表单。第一个是今天的日期或者我们可以选择的日期,第二个是第一天加上一个星期,7天。
我能够让它们最初是正确的。
$scope.first = new Date();
$scope.second = new Date().setDate( new Date().getDate() + 7);
但是,如果我更改第一个日期选择器的日期,第二个日期选择器将不会做任何更改。我找到了 {{dt | date:'fullDate' }}
捕获动态日期。我的问题是如何让第一个日期选择器的日期成为一个变量并在我的 .js 文件中进行操作?然后我可以在第一个日期更改时向我的第二个日期选择器添加 +7 天。
最佳答案
快速解决。在第一次约会时使用 watcher。每当第一个日期更改时,第二个日期增加 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>
关于javascript - Angular UI Bootstrap Datepicker 动态日期(可变),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36417392/