【发布时间】: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