2016-05-01 37 views
2

我正在嘗試獲取用戶輸入的手動輸入。但是當我輸入日期爲dd/MM/yyyy時,它始終爲NULL。它適用於我給dd/MM/yyyy HH:mm。我的onChange()函數不會調用。Angular UI DateTime Picker不需要手動輸入

角JS代碼:

(function() { 
 
    'use strict'; 
 

 
    /** 
 
    * My Controller 
 
    */ 
 
    angular.module('test').controller('MyController', MyController); 
 
    // The $inject property is an array of service names to inject. 
 
    MyController.$inject = ['GenericGetService', 'GenericPostService', '$routeParams', '$location', '$rootScope']; 
 
    function MyController(GenericGetService, GenericPostService, $routeParams, $location, $rootScope) { 
 
    var myController = this; 
 
    myController.openCalendar = openCalendar; 
 
    myController.isOpen = false; 
 
    myController.onChange = onChange; 
 
    myController.myDate = null; 
 

 
    function openCalendar(e) { 
 
     e.preventDefault(); 
 
     e.stopPropagation(); 
 

 
     myController.isOpen = true; 
 
    }; 
 

 
    function onChange() { 
 
     console.log(myController.myDate); 
 
    } 
 
    } 
 

 
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css"> 
 
<p class="input-group"> 
 
    <input type="text" id="myDate" initialPicker="date" ng-change="onChange()" class="form-control" datetime-picker="dd/MM/yyyy HH:mm" defaultTime="00:00:00" data-ng-model="myController.myDate" is-open="myController.isOpen" /> 
 
    <span class="input-group-btn"> 
 
    <button type="button" class="btn btn-default" ng-click="myDate.openCalendar($event)"><i class="fa fa-calendar"></i></button> 
 
    </span> 
 
</p>

這裏是Demo Code

代碼如何解決這個問題?如何使它同時適用於dd/MM/yyyy和dd/MM/yyyy HH:mm格式?

注:使用JavaScript,我可以得到價值

var data = document.getElementById('myDate').value; 
myController.myDate = new Date(data); 

回答

0

我看到你當前庫已經支持它 只是添加alt-input-formats="['MM/dd/yyyy HH:mm', 'MM/dd/yyyy']"在HTML 或者你可以在這裏嘗試一個活生生的例子 https://plnkr.co/edit/QUgR6r?p=preview