2016-09-15 22 views
1

我有一个带有datePicker的输入类型文本。格式ng模型的初始值(使用datePicker的输入类型文本)

<input id="start-date" style="width: 230px;" close-text="Close" 
type="text" ng-model="startDate" datepicker-popup="dd-MM-yyyy" 
ng-required="true"/></div> 

当输入显示时,我必须显示它的初始值。问题是,该值显示格式为:

Wed Jun 15 2016 10:48:49 GMT+0000 (WET)

它只是选择从我可以看到我想要的(DD-MM-YYYY)格式的日期选择一个日期之后。为了解决这个问题,我补充说:

$scope.$watch('startDate', function (newValue) { 
    $scope.startDate = $filter('date')(newValue, 'dd-MM-yyyy'); 
}); 

但我得到这个错误:日期选择器指令:“NG-模式”的值必须是Date对象,一些因为01.01.1970或字符串毫秒表示RFC2822或ISO 8601的日期。

我怎样才能解决这个问题呢?

谢谢!

+0

在NG-模式?像这样:'ng-model =“startDate | date”'? –

+0

你使用AngularUI datepicker? – Rakeschand

回答

0

假设你正在使用的AngularUI日期选择和你的NG-型号初始值没有被格式化只需添加下面的指令到您的项目将解决该问题:

directive('datepickerPopup', function(){ 
    return { 
    restrict: 'EAC', 
    require: 'ngModel', 
    link: function(scope, element, attr, controller) { 
     //remove the default formatter from the input directive to prevent conflict 
     controller.$formatters.shift(); 
    } 
    } 
}) 
+0

谢谢你的回复,但是我已经在ui-bootstrap-tpls.js文件中有了这个指令,但是我没有这个控制器。$ formatters.shift();' –

+0

我要不要在这里添加它文件? –

+0

只需添加一个新的文件作为一个新的指令,就像你的控制器 – Rakeschand