【问题标题】:How to get the date time value from input如何从输入中获取日期时间值
【发布时间】:2017-06-23 03:21:09
【问题描述】:

我在我的 Angular 项目中使用来自 github 的 datetimepicker library。当我选择日期和时间时它工作正常。但是当我将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

【问题讨论】:

    标签: javascript angularjs angular-bootstrap


    【解决方案1】:

    你可以很好地使用它

    $("#example-datetimepicker5").on("dp.change", function() {
    
        $scope.selecteddate = $("#example-datetimepicker5").val();
        alert("selected date is " + $scope.selecteddate);
    
    });
    

    【讨论】:

    • 我正在使用 angularJS,jQuery '$' 应该忽略。
    • 可以在angularjs中使用JQuery,但需要为其添加Lib
    猜你喜欢
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    相关资源
    最近更新 更多