Javascript 启动日期选择器格式不适用于初始化
我试图在我的应用程序中使用angular bootstrap datepicker模块,但遇到了一个小问题。我使用输入文本和按钮显示日期,如下所示:Javascript 启动日期选择器格式不适用于初始化,javascript,angularjs,datepicker,angular-ui,angular-ui-bootstrap,Javascript,Angularjs,Datepicker,Angular Ui,Angular Ui Bootstrap,我试图在我的应用程序中使用angular bootstrap datepicker模块,但遇到了一个小问题。我使用输入文本和按钮显示日期,如下所示: <div class="row" id="datePicker"> <p class="input-group"> <input type="text" class="form-control" datepicker-popup="{{format}}" ng-model="currentDat
<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日
编辑2来自camden_kid worked!提供的链接的解决方案:)详细信息见他的评论。答案可在此处找到: 我手动格式化了初始化日期,如下所示:
$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);
});
}
};