简体   繁体   English

AngularJS不会更改Datepicker日期格式

[英]Angularjs doesn't change datepicker date format

I tried many solutions from this site marked as anwers still the datepicker doesn't change the date format. 我尝试了从此站点标记为答案的许多解决方案,但datepicker不会更改日期格式。 i'm using this angular directive with no results: 我正在使用此角度指令,但没有结果:

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();
               }
           });
       }
    });

also on the html: 也在html上:

 <input type="text" ng-model="finalDate" date-format="dd/mm/yyyy" date-type="string" datepicker>

nothing either, added this code in the script: 也没有,在脚本中添加了以下代码:

  $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)
    })

the console log prints nothing. 控制台日志不打印任何内容。 Honestly i don't know what else to do, i tried other solutions with no result. 老实说,我不知道该怎么办,我尝试了其他解决方案,但没有结果。 any ideas? 有任何想法吗?

EDIT: Plunker added http://plnkr.co/edit/hoGYFslN3KovH0wkj5kE?p=preview 编辑: Plunker添加了http://plnkr.co/edit/hoGYFslN3KovH0wkj5kE?p=preview

Use this directive 使用此指令

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();
        });
    }
}}]);

if you want to use your directive, it should be like 如果您想使用您的指令,它应该像

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();
        }
    });
}});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM