gpt4 book ai didi

angularjs - 绑定(bind) Angular 日期选择器和时间选择器

转载 作者:行者123 更新时间:2023-12-02 23:06:02 25 4
gpt4 key购买 nike

我想绑定(bind) Angular 时间选择器和日期选择器。我正在使用Angular bootstrap library

根据 timepicker 的文档,它需要 new Date 对象作为最小值和最大值。如果我只是这样设置

var d = new Date();
d.setHours( 2 );
d.setMinutes( 0 );
$scope.minTime = d;

var d = new Date();
d.setHours( 11 );
d.setMinutes( 0 );
$scope.maxTime = d;

<uib-timepicker ng-model="mytime" min="min" max="max" show-meridian="false" min="minTime" max="maxTime"></uib-timepicker>

由于 new Date() 对象,这只适用于今天

我希望它在我选择的任何一天都能工作。目前,timepicker 并未按要求限制时间。另外,有时时间选择器框边框会变成红色(可能是由于验证错误),但没有指定错误到底是什么。

请看一下: http://plnkr.co/edit/LOZ9T6zexRkQpqSIaJiA?p=preview

最佳答案

为了根据所选日期初始化timepicker,请将dateChange函数替换为:

$scope.dateChange = function() {
var selectedDate = $scope.dt;
var min = new Date(selectedDate.getTime());
min.setHours(2);
min.setMinutes(0);
$scope.min = min;

var max = new Date(selectedDate.getTime());
max.setHours(4);
max.setMinutes(0);
$scope.max = max;
}

工作示例

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('DatepickerDemoCtrl', function($scope) {
$scope.format = 'yyyy/MM/dd';
$scope.min = null;
$scope.max = null;


$scope.initTimePicker = function(selectedDate) {
var min = new Date(selectedDate.getTime());
min.setHours(2);
min.setMinutes(0);
$scope.min = min;

var max = new Date(selectedDate.getTime());
max.setHours(4);
max.setMinutes(0);
$scope.max = max;
}


$scope.init = function() {
$scope.dt = new Date();
$scope.initTimePicker($scope.dt);
};
$scope.init();

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

$scope.open = function() {
$scope.popup.opened = true;
};


$scope.popup = {
opened: false
};



$scope.dateChange = function() {
$scope.initTimePicker($scope.dt);
}


});
<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.4.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">


<div ng-app="ui.bootstrap.demo" ng-controller="DatepickerDemoCtrl">

<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="popup.opened" datepicker-options="dateOptions"
ng-required="true" close-text="Close" ng-change="dateChange()" />

<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open()"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
</div>
</div>

<uib-timepicker ng-model="dt" hour-step="1" minute-step="15" name="sTime" show-meridian="true" min="min" max="max">
</uib-timepicker>

<pre class="alert alert-info">Time is: {{dt | date:'M/d/yyyy HH:mm' }}</pre>


</div>

Plunker

关于angularjs - 绑定(bind) Angular 日期选择器和时间选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35933473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com