如何设置输入的ng模型以使用auto-binding
更改Date and Time
?
这是普伦克 http://plnkr.co/edit/7kuvF6
在这种情况下,我想更改日期和时间,但我不知道如何为两个输入设置ng-model
。
谢谢!
以下是设置日期和时间ng-model
的方法:
在您的 html 文件中:
<div ng-controller="date">
DATE <input type="text" ng-model="date" jqdatepicker/><br />
TIME <input type="text" ng-model="time" /><br />
DateTTime: {{dateTtime}}<br />
</div>
在您的指令中:
myAPP.directive('jqdatepicker', function () {
return {
restrict: 'A',
link: function (scope, element, attrs) {
element.datepicker({
changeYear: "true",
changeMonth: true,
dateFormat: 'yy-mm-dd',
showOtherMonths: true,
showButtonPanel: true,
onClose: function (selectedDate) {
scope.dateTtime = selectedDate + "T" + scope.time;
scope.$apply();
}
});
}
};
});
我假设您想保持2013-10-01T00:00
格式,或者将以这种格式从某处接收数据。
工作中的更多详细信息 Plunker: http://plnkr.co/edit/P7BG6q?p=preview
您可以将其分配给日期选取器的回调函数的作用域中定义的模型。您还需要scope.$apply()
来触发摘要,因为赋值在 AngularJS 之外。
onClose: function (selectedDate) {
scope.dateTtime = selectedDate;
scope.$apply();
}