3

我想在我的應用程序中使用角引導datepicker模塊,遇到小問題。我使用輸入文本和按鈕來顯示日期是這樣的:引導日期選擇器格式不起作用初始化

<div class="row" id="datePicker"> 
    <p class="input-group"> 
     <input type="text" class="form-control" datepicker-popup="{{format}}" ng-model="currentDate" 
     is-open="opened" datepicker-options="dateOptions" date-disabled="disableDt(date, mode)" 
     ng-required="true" close-text="Close" show-button-bar="false" ng-init="" readonly/> 
     <span class="input-group-btn"> 
      <button type="button" class="btn btn-default" ng-click="open($event)"> 
       <i class="glyphicon glyphicon-calendar"></i> 
      </button> 
     </span> 
    </p> 
</div> 

由於使用角谷歌地圖我必須手動引導我的應用程序。似乎由於手動引導,datepicker無法正確格式化日期並顯示整個未格式化的日期。當我從datepicker中選擇任何日期時,日期將在輸入字段中正確顯示。更重要的是,模型更改不會強制格式發生變化(例如,最初我從json文件獲得了一些日期,並且它們顯示在字段中,但沒有格式化)。下面 實施例:

  • 初始日期:週五2014年1月17日01:00:00 GMT + 0100(CET)
  • 預期日期(和一個選擇的同一天后顯示):2014 1月17日

有什麼辦法刷新這個小部件,所以它知道在開始正確的格式或在恰當的時刻更改它適當初始化?

編輯:當我將datepicker移動到fragment時,它應該沒有未初始化模型的問題(此片段稍後加載)。仍然會出現問題 - 日期未格式化,似乎根本沒有與格式化程序或模型相關聯(例如,使用格式下拉菜單和選擇不同的值,直到在datepicker中選擇日期才能使用教程)。

編輯2由camden_kid提供的鏈接解決方案工作! :)在他的評論中的細節。

+0

我的問題可能是一些幫助http://stackoverflow.com/questions/25742445/angularjs-1-3-datepicker-initial-format-is-incorrect – 2014-11-10 20:27:08

+0

謝謝你,從該問題的解決方案之一工作! :) 準確地說 - 我決定在初始化時手動格式化變量。 – SzybkiSasza 2014-11-10 20:40:31

+0

很高興能有所幫助。您可能想要提出問題和答案。乾杯。 – 2014-11-10 20:51:52

回答

5

答案可以在這裏找到: AngularJS 1.3 - Datepicker initial format is incorrect

我手動格式化在初始化的日期如下:

$scope.currentDate = $filter('date')(new Date(), $scope.format); 

然而,更好的解決方案將是超載指令如下(從原來的鏈接採取):

angular.module('myApp').directive('datepickerPopup', function (dateFilter, datepickerPopupConfig) { 
return { 
    restrict: 'A', 
    priority: 1, 
    require: 'ngModel', 
    link: function(scope, element, attr, ngModel) { 
     var dateFormat = attr.datepickerPopup || datepickerPopupConfig.datepickerPopup; 
     ngModel.$formatters.push(function (value) { 
      return dateFilter(value, dateFormat); 
     }); 
    } 
}; 

});

當日期由事後日期選擇器改變,日期選擇器本身處理的任何日期格式:)

感謝@camden_kid提供的鏈接! :)