解析ui.bootstrap.datepicker对象

时间:2015-03-09 03:33:57

标签: javascript angularjs angularjs-directive angularjs-scope angular-ui

我为ui.bootstrap.datepicker编写了一个指令。 (http://angular-ui.github.io/bootstrap/)它运行良好,但是一旦选择了日期,它就不会将日期对象解析回控件。日期选择后不会应用日期格式。

日期格式未应用。 enter image description here

应用日期格式(我希望它是这个) enter image description here

当我调试时,我可以看到两个不同的对象。

  1. " Thu Aug 28 2014 08:00:00 GMT + 0800(W. Australia Standard Time)"
  2. " 2014-08-28T00:00:00"
  3. 有什么想法吗?

    app.directive('reusablePicker', function ($compile) {
    
    var template =
         '        <div class="input-group">'+
         '             <input type="text" class="form-control datepicker" datepicker-popup="dd-MMMM-yyyy" ng-model="pickerDate" is-open="dateOpened" ng-focus="dateOpen($event)" close-text="Close" ng-disabled="disabled" />' +
         '             <div class="input-group-addon" ng-click="dateOpen($event)" ' +
         '                 <a href="#"><i class="linecons-calendar"></i></a>'+
         '             </div>' +
         '         </div>';
    
    var linker = function (scope, element, attrs, ngModelCtrl) {
        scope.disabled = attrs.ngDisabled;
    
        ngModelCtrl.$render = function () {
            scope.pickerDate = ngModelCtrl.$viewValue;
        };
    
        scope.$watch('pickerDate', function () {
            ngModelCtrl.$setViewValue(scope.pickerDate);
        });
    
        element.html(template).show();
        $compile(element.contents())(scope);
    };                    
    
    return {              
        require: 'ngModel',
        link: linker,
        replace: true,
        restrict: 'EA',
        scope: {},
        controller: ['$scope', function ($scope) {
    
            $scope.dateOpen = function ($event) {
                if ($scope.disabled !== "true") {
                    $event.preventDefault();
                    $event.stopPropagation();
                    $scope.dateOpened = true;
                }
            };
    
        }]                
    };                    
    

    });

1 个答案:

答案 0 :(得分:0)

    ngModelCtrl.$parsers.push(function (modelValue) {

        var date = new Date(modelValue).toISOString();
        return date;
    });