2015-07-12 54 views
1

我有一個輸入字段類型的日期。 AngularJS有日期選擇器。當用戶編輯已經保存的表單時,AngularJS會拋出一個錯誤,並且根本沒有設置日期,它在UI中是空的。我非常感謝您的幫助!AngularJS:如何自動選擇日期

HTML

<div class="col-md-10"> 
     <input type="date" name="purchaseDate" class="form-control" ng-model="rma.purchaseDate" placeholder="{{translation.DATEOFPERMIT_PLACEHOLDER}}"> 
</div> 

控制器

//In mySQL DB format is: 2015-07-02 
$scope.rma.purchaseDate = $filter("date")(new Date(rma.purchaseDate).toISOString(), 'dd-MM-yyyy'); 
console.log($scope.rma.purchaseDate); 

中的console.log

Error: [ngModel:datefmt] Expected 02-07-2015 to be a date

The specified value '02-07-2015' does not conform to the required format, 'yyyy-MM-dd'.

在UI

在UI格式爲DD/MM/YYYY

UPDATE

我只是把過濾掉,並試圖很簡單的辦法,這是工作:

$scope.rma.purchaseDate = new Date(rma.purchaseDate); 

謝謝大家!

+0

你正在使用哪個datepicker?鏈接? –

+0

什麼是在UI上顯示的格式?日期選擇器? –

+1

據我所知,角度1.3 +的ngModel與類型=「日期」需要日期爲真正的日期對象(角1.2不是)和HTML5期望它作爲字符串。所以你需要ngModel $ formatters像http://stackoverflow.com/questions/14474555/how-to-format-a-date-using-ng-model – YOU

回答

0

您需要提供一個有效的JavaScript對象數據輸入的ng-model

這裏是DOC

它說,

The model must always be a Date object, otherwise Angular will throw an error. Invalid Date objects (dates whose getTime() is NaN) will be rendered as an empty string.

這樣做,你可以創建一個javascriptdate像,

//In mySQL DB format is: 2015-07-02 
var dateVal = '2015-07-02'; 
var datePartials = dateVal.split("-"); 
var dateModel = new Date(datePartials[0], datePartials[1] - 1, datePartials[2]); 

$scope.rma.purchaseDate = dateModel; 

這裏是一個DEMO

相關問題