最初在范围内设置值时,Datepicker-popup格式化不起作用

Nia*_*h14 11 html javascript angularjs angularjs-directive angular-ui-bootstrap

我在Plunker上使用此自定义指令使用Angular UI引导日期选择器弹出窗口(http://plnkr.co/edit/053VJYm1MpZUiKwFTfrT?p=preview):

//Module
var userModule = angular.module("userModule",['ui.bootstrap']);

//Controller
userModule.controller("sampleController", ['$scope', function ($scope) {
    $scope.minDate = new Date();
}]);

//Directive code
userModule.directive('datePicker', [function (dateFilter) {
    return {
        restrict: 'E',
        require: 'ngModel',
        scope: {
            ngModel: '=',
            ngReadonly: '=?',
            minDate: '=?',
            maxDate: '=?',
            dtpRequired: '=?',
            dateOptions: '=?'
        },
        template: '<p class="input-group">' +
                    '<input type="text" style="cursor:pointer" class="form-control" datepicker-popup="{{format}}"' +
                        'ng-model="ngModel" is-open="opened"' +
                            'min-date="minDate" max-date="maxDate"' +
                                'datepicker-options="dateOptions" date-disabled="disabled(date, mode)"' +
                                 'ng-required="dtpRequired" close-text="Close" ng-readonly="ngReadonly" ng-click="openPopup()" />' +
                         '<span class="input-group-btn">' +
                            '<button type="button" class="btn btn-default" ng-click="openPopup($event)">' +
                                '<i class="fa fa-calendar"></i></button>' +
                        '</span>' +
                    '</p>',
        controller: function ($scope) {
            // check if it was defined.  If not - set a default
            $scope.dateOptions = $scope.dateOptions || {
                formatYear: 'yy',
                startingDay: 1,
                showWeeks: false
            };

            $scope.openPopup = function ($event) {
                if ($event !== undefined) {
                    $event.stopPropagation();
                }
                $scope.opened = true;
            };

            $scope.format = 'dd MMMM yyyy';
        },
        link: function ($scope, element, attrs, controller) {
            //remove the default formatter from the input directive to prevent conflict
            controller.$formatters.shift();
        }
    };
}]);
Run Code Online (Sandbox Code Playgroud)

这工作正常,从日历弹出窗口中选择日期时日期格式正常.但是,如果我在控制器中设置了ng-model的日期,则日期不会被格式化为'dd MMMM yyyy',而是作为日期字符串返回,例如Sat Oct 01 2016 01:00:00 GMT + 0100(GMT Daylight)时间).但是在Plunker中,我可以在控制器中设置日期并且格式正常.这是我的日期选择器的HTML:

<date-picker ng-model="startDate.value" datepicker-options="dateOptions" min-date="minDate" ng-readonly="true"></date-picker>
Run Code Online (Sandbox Code Playgroud)

在我的控制器中 startDate.value = new Date();

我不确定问题出在哪里.下图显示了我要回来的内容.

错误的日期

Nia*_*h14 1

终于找到了解决这个问题的方法。我从 Angular-UI-Bootstrap 更改为 uib-datepicker 指令,现在当在控制器中设置日期时,它可以正确格式化日期!这是我的指令中的 HTML 模板,供参考:

template: '<p class="input-group">' +
            '<input type="text" style="cursor:pointer" class="form-control" uib-datepicker-popup="{{format}}"' +
                'ng-model="ngModel" is-open="opened"' +
                    'min-date="minDate" max-date="maxDate"' +
                        'datepicker-options="dateOptions"date-disabled="disabled(date, mode)"' +
                         'ng-required="dtpRequired" close-text="Close" ng-readonly="ngReadonly" ng-click="openPopup()" />' +
                 '<span class="input-group-btn">' +
                    '<button type="button" class="btn btn-default" ng-click="openPopup($event)">' +
                        '<i class="fa fa-calendar"></i></button>' +
                '</span>' +
            '</p>'
Run Code Online (Sandbox Code Playgroud)