我尝试了许多来自此网站标记为anwers的解决方案仍然是datepicker不会更改日期格式。我正在使用这个角度指令而没有结果:
App.directive('datepicker', function() {
return function(scope, element, attrs) {
element.datepicker({
inline: true,
dateFormat: 'dd/mm/yy',
onSelect: function(dateText) {
var modelPath = $(this).attr('ng-model');
putObject(modelPath, scope, dateText);
scope.$apply();
}
});
}
});
也在html上:
<input type="text" ng-model="finalDate" date-format="dd/mm/yyyy" date-type="string" datepicker>
什么都没有,在脚本中添加了这段代码:
$scope.$watch('datepicker.date', function(v){ // using the example model from the datepicker docs
var d = new Date(v);
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
$scope.modDate = curr_date + "/" + curr_month + "/" + curr_year;
console.log($scope.modDate)
})
控制台日志不打印任何内容。老实说,我不知道还能做什么,我尝试了其他没有结果的解决方案。任何想法?
编辑: Plunker添加了http://plnkr.co/edit/hoGYFslN3KovH0wkj5kE?p=preview
答案 0 :(得分:0)
使用此指令
App.directive('datepicker', ['$filter', '$parse', function ($filter, $parse) {
return {
require: 'ngModel',
link: function (scope, element, attrs, ctrl) {
element.datetimepicker({
weekStart: 1,
todayBtn: 1,
autoclose: 1,
todayHighlight: 1,
startView: 2,
minView: 2,
forceParse: 0,
endDate: attrs.enddate == "" ? new Date() : null,
format: "yyyy-MM-dd"
}).on('changeDate', function (ev) {
var dateUTC = new Date(ev.date.getTime() + (ev.date.getTimezoneOffset() * 60000));
var filterApply = $filter('date')(dateUTC, 'yyyy-MM-dd');
$parse(attrs.ngModel).assign(scope, filterApply);
scope.$digest();
});
}
}}]);
如果你想使用你的指令,它应该像
App.directive('datepicker', function ($filter, $parse) {
return function (scope, element, attrs) {
element.datepicker({
inline: true,
dateFormat: 'dd/mm/yy',
onSelect: function (dateText) {
var dateUTC = new Date(dateText.date.getTime() + (dateText.date.getTimezoneOffset() * 60000));
var filterApply = $filter('date')(dateUTC, 'dd/mm/yy');
$parse(attrs.ngModel).assign(scope, filterApply);
scope.$digest();
}
});
}});