我有一个新手AngularJS问题 - 我正在使用datetimepicker库,我很难将选择的日期绑定到我的模型......我看到一些帖子谈论无法访问datetimepicker模型直接,但在我的情况下,我发现如果我只是在我的日期输入框中手动输入文本值,它会绑定!如果我选择选择器的日期,我会得到可怕的' undefined' :( ...帮忙?
HTML标记:
<div class="col-md-2">
<div name="fromDateTime" class="input-group date" id="datetimepickerFrom">
<input class="form-control" type="text" name="fromDateTime" ng-model="formEntries.fromDateTime"/>
<span class="input-group-addon" data-ng-click="pickFromDateTime()"><span class="glyphicon glyphicon-calendar" id="calIconFrom"></span></span>
</div>
</div>
<div class="col-md-2">
<div name="toDateTime" class="input-group date" id="datetimepickerTo">
<input class="form-control" type="text" name="toDateTime" ng-model="formEntries.toDateTime"/>
<span class="input-group-addon" data-ng-click="pickToDateTime()"><span class="glyphicon glyphicon-calendar"></span></span>
</div>
</div>
控制器:
$scope.pickFromDateTime = function () {
$("#datetimepickerFrom").datetimepicker();
$("#datetimepickerFrom").datetimepicker().change(function() {
$(this).data("DateTimePicker").hide();
console.log($(this).data("DateTimePicker").getDate());
});
}
$scope.pickToDateTime = function () {
$("#datetimepickerTo").datetimepicker();
$("#datetimepickerTo").datetimepicker().change(function() {
$(this).data("DateTimePicker").hide();
console.log($(this).data("DateTimePicker").getDate());
});
}
$scope.getData = function() {
console.log($scope.formEntries.fromDateTime);
console.log($scope.formEntries.toDateTime);
}
答案 0 :(得分:1)
永远不要在控制器中编写DOM或jQuery代码。它永远不会正常工作。您必须创建一个指令来执行此操作。该指令将监听您正在使用的任何库中的事件并更新您的模型。 Here is an example of a directive that happens to work with the datepicker。这可以让你走上正确的轨道。