【问题标题】:Bind angular datepicker and timepicker绑定角度日期选择器和时间选择器
【发布时间】:2016-03-11 06:13:40
【问题描述】:

我想绑定角度时间选择器和日期选择器。我正在使用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 没有按要求限制时间。此外,有时 timepicker 框的边框会变成红色(可能是由于验证错误),但没有具体说明错误的原因。

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

【问题讨论】:

  • 试试这个:$scope.today = function() { $scope.dt = new Date("21/10/2014"); };
  • 我想动态设置它,无论我从日期选择器中选择哪个日期,时间选择器的最小最大时间应该设置为 2 到 4...

标签: angularjs angular-bootstrap


【解决方案1】:

为了根据选择的日期初始化timepickerdateChange函数替换为:

$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

【讨论】:

  • 查看日期何时更改。时间选择器上的框变成红色(可能是由于验证)。此外,Min max 现在不适用于默认日期。
  • 该示例已更新为为默认日期初始化时间选择器。由于我们指定了 min 和 max 属性,一旦当前时间设置为超出时间间隔,Timepicker 按钮就会变为红色
  • 非常感谢!工作正常。但是我有 1 个更改,$scope.dt = new Date() 然后 $scope.dt.setHours(2);
  • stackoverflow.com/questions/46428210/… 可以看看这个链接
猜你喜欢
  • 1970-01-01
  • 2020-02-17
  • 2020-04-23
  • 1970-01-01
  • 2012-09-11
  • 1970-01-01
  • 2018-09-13
  • 1970-01-01
  • 2013-10-07
相关资源
最近更新 更多