如何从输入中获取日期时间值

How to get the date time value from input

我正在为我的 angular 项目使用 github 中的 datetimepicker library。当我 select 日期和时间时它工作正常。但是当我将 ng-model 添加到输入字段并检索它时,输入字段显示为空。它不会根据 ng-model 更新字段。

指令

App.directive('jsDatetimepicker', function () {
 return {
    link: function (scope, element, attrs) {
        var options = (typeof scope.$eval(attrs.jsDatetimepicker) !== 'undefined') ? scope.$eval(attrs.jsDatetimepicker) : new Object();

        jQuery(element).datetimepicker({
            format: options.format ? options.format : false,
            useCurrent: options.useCurrent ? options.useCurrent : false,
            locale: moment.locale('' + (options.locale ? options.locale : '') +''),
            showTodayButton: options.showTodayButton ? options.showTodayButton : false,
            showClear: options.showClear ? options.showClear : false,
            showClose: options.showClose ? options.showClose : false,
            sideBySide: options.sideBySide ? options.sideBySide : false,
            inline: options.inline ? options.inline : false,
            icons: {
                time: 'si si-clock',
                date: 'si si-calendar',
                up: 'si si-arrow-up',
                down: 'si si-arrow-down',
                previous: 'si si-arrow-left',
                next: 'si si-arrow-right',
                today: 'si si-size-actual',
                clear: 'si si-trash',
                close: 'si si-close'
            }
        });
    }
 };
});

HTML

<input id="example-datetimepicker5" data-js-datetimepicker="" type="text" name="example-datetimepicker5" placeholder="Choose a date.." ng-model="news.new_date" class="form-control"/>
<label for="example-datetimepicker5">Date</label>
<button ng-click="CreateNews()" type="button" class="btn btn-sm btn-primary"><i class="fa fa-check"></i> Create</button>

控制器

$scope.news = {};
$scope.news.new_date = "";
$scope.CreateNews = function(){
 alert(JSON.stringify($scope.news.new_date));
}

输出

new_date returns empty

预计

new_date returns datetime

你可以很好地使用这个

$("#example-datetimepicker5").on("dp.change", function() {

    $scope.selecteddate = $("#example-datetimepicker5").val();
    alert("selected date is " + $scope.selecteddate);

});