【问题标题】:Angular UI Bootstrap Datepicker dynamic date (variable)Angular UI Bootstrap Datepicker 动态日期(变量)
【发布时间】:2016-04-05 04:54:00
【问题描述】:

我正在使用Angular UI Bootstrap 并创建一个包含两个日期选择器的表单。第一个是今天的日期或我们可以选择的日期,第二个是第一天加上一周,7天。

我能够让它们最初是正确的。

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

但是,如果我更改第一个 datepicker 的日期,第二个不会做任何更改。我发现{{dt | date:'fullDate' }} 捕捉到了动态日期。我的问题是如何让第一个 datepickers 的日期成为一个变量并在我的 .js 文件中进行操作?然后我可以在第一个日期更改时向我的第二个日期选择器添加 +7 天。

Sample code here

【问题讨论】:

    标签: javascript angularjs twitter-bootstrap datepicker angular-ui-bootstrap


    【解决方案1】:

    快速解决方案。在第一次约会时使用观察者。每当第一个日期更改时,将 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>

    【讨论】:

      【解决方案2】:

      如果您正在寻找 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>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-03
        相关资源
        最近更新 更多