【发布时间】:2014-02-23 16:09:11
【问题描述】:
我有点迷路了。我定义了一个创建日期选择器的角度指令(eternicode(https://github.com/eternicode/bootstrap-datepicker/) 的引导日期选择器):
日期选择器似乎可以直观地工作。它会弹出,并在我的文本框中输入一个值。但是除非我手动编辑文本框,否则角度模型不会更新。
我的指令:
myApp.directive('ccDatePicker', function ($compile) {
return {
restrict: 'EA',
template: '<input type="text" ng-model="ngModel" />',
require: '^ngModel',
scope: {
ngModel: '='
},
replace: false ,
link: function (scope, element, attrs) {
element.children(0)
.on('change', function () { scope.$digest(); })
.datepicker({ format: 'yyyy/mm/dd', autoclose: true });
}
};
});
我的 HTML:
<label>Due date: <cc-date-picker ng-model="todoObject.dueBy" /></label>
<div>
dueBy: {{todoObject.dueBy}} (updates when editing textbox by hand, but not when using date picker)
</div>
我使用了 scope.$digest 来刷新模型,我也尝试了 scope.$apply。没有运气。
当用户在日期选择器中选择日期时,如何更新我的角度模型?
【问题讨论】:
标签: angularjs datepicker