【问题标题】:Angular-ui-bootstrap datepicker is setting default calendar date to December 31 1969 when using with ng-required与 ng-required 一起使用时,Angular-ui-bootstrap datepicker 将默认日历日期设置为 1969 年 12 月 31 日
【发布时间】:2021-04-07 22:19:04
【问题描述】:

我有一个表单,其中有一个条件需要的 datepicker 元素。我无法使用任何值启动此元素,因为用户必须输入它。日期选择器格式为 dd/mm/yyyy。问题是在我添加ng-required 之前它工作正常。在元素中添加ng-required 条件后,当用户单击日历时,日历默认日期为 1969 年 12 月 31 日。用户将该日期更改为当前日期非常不方便。我尝试在日期选择器选项中将默认日期设置为今天,但没有运气。

<div class="form-group col-md-3 required" ng-show="decision==true">
  <label class="control-label">Test Date</label>
  <div class="input-group">
    <input name="testDate" type="text" class="form-control" datepicker-popup="dd/MM/yyyy" ng-model="testDate" is-open="dpOpened.testDateTo"
    close-text="Close" onfocus="this.blur();" datepicker-options="dateOptions" ng-required="decision==true" required/>
    <span class="input-group-btn">
    <button type="button" class="btn btn-default" ng-click="datepickerPopup($event, 'testDateTo')">
      <i class="glyphicon glyphicon-calendar"></i>
    </button>
    </span>
  </div>
</div>

--controller.js--

$scope.datepickerPopup = function($event, opened) {
  $event.preventDefault();
  $event.stopPropagation();
  $scope.dpOpened[opened] = true;
}

$scope.dateOptions = {
  showWeeks : false,
  defaultDate : new Date()
};

$scope.dpOpened = {
  testDateTo : false,
};

$scope.today = function() {
  return new Date();
}

【问题讨论】:

    标签: angularjs datepicker angular-ui-bootstrap


    【解决方案1】:

    您已将 ng-model 设置为 testDate,但我看不到您在控制器中的哪个位置进行了设置。尝试设置 testDate = new Date()。看到这个相关的pull request

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。

      您不应该使用 new Date() 初始化 bsDatePicker 字段。您必须使用 formbuilder 中的“null”进行初始化,即使它是必填字段。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-17
        • 1970-01-01
        • 2018-04-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多