我有一个表单,需要为不同的事情设置 2 个或更多日期字段。我尝试了 Angular UI Bootstrap,当表单中只有 1 个日期字段时,它工作得很好。但如果我有多个日期字段,并且我不知道更简单的方法来使其工作,它就会停止工作。
这是我的 HTML 示例:
<label>First Date</label>
<div class="input-group">
<input type="text" class="form-control" datepicker-popup="{{format}}" name="dt" ng-model="formData.dt" is-open="opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" />
<span class="input-group-btn">
<button class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</div>
<label>Second Date</label>
<div class="input-group">
<input type="text" class="form-control" datepicker-popup="{{format}}" name="dtSecond" ng-model="formData.dtSecond" is-open="opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" />
<span class="input-group-btn">
<button class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</div>
我的JS是:
myApp.controller('DatePickrCntrl', function ($scope) {
$scope.today = function() {
$scope.formData.dt = new Date();
};
$scope.today();
$scope.showWeeks = true;
$scope.toggleWeeks = function () {
$scope.showWeeks = ! $scope.showWeeks;
};
$scope.clear = function () {
$scope.dt = null;
};
// Disable weekend selection
$scope.disabled = function(date, mode) {
return ( mode === 'day' && ( date.getDay() === 0 || date.getDay() === 6 ) );
};
$scope.toggleMin = function() {
$scope.minDate = ( $scope.minDate ) ? null : new Date();
};
$scope.toggleMin();
$scope.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};
$scope.dateOptions = {
'year-format': "'yy'",
'starting-day': 1
};
$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'shortDate'];
$scope.format = $scope.formats[0];
});
我根据示例实现了here http://angular-ui.github.io/bootstrap/。我这里遇到的问题是:
1)当单击其中一个日期字段时,弹出的日期选择器很混乱,似乎在 1 中显示 2 个日期选择器。
2)当我删除时is-open="opened"
属性,弹出窗口似乎工作正常。但没有is-open="opened"
, the ng-click="open($event)
因为按钮不起作用。
3)由于每个日期字段都有不同的 ng-models,因此我无法为任何日期字段设置默认日期,除了第一个日期字段ng-model="formData.dt"
我能想到的解决这个问题的唯一长方法是分离每个日期字段的控制器。
我想知道其他人在使用 Angular UI Bootstrap 时如何在 1 个表单中实现多个日期字段。