diff --git a/demo/demo.html b/demo/demo.html index de27a50..74e26b8 100644 --- a/demo/demo.html +++ b/demo/demo.html @@ -17,5 +17,6 @@
{{aDate}}
+
Increase Date
\ No newline at end of file diff --git a/src/date.js b/src/date.js index 689259f..a5d2740 100644 --- a/src/date.js +++ b/src/date.js @@ -62,6 +62,14 @@ angular.module('ui.date', []) } element.datepicker("setDate", date); }; + + scope.$watch(attrs.ngModel, function() { + var date = controller.$viewValue; + if ( angular.isDefined(date) && date !== null && !angular.isDate(date) ) { + return; + } + controller.$render(); + }, true); } // If we don't destroy the old one it doesn't update properly when the config changes element.datepicker('destroy'); diff --git a/test/date.spec.js b/test/date.spec.js index 7d469d4..9b6e5bf 100644 --- a/test/date.spec.js +++ b/test/date.spec.js @@ -26,6 +26,19 @@ describe('uiDate', function() { expect(element.datepicker('getDate')).toEqual(aDate); }); }); + it('should update the datepicker if the model changes', function() { + inject(function($compile, $rootScope) { + var aDate, element; + aDate = new Date(2010, 12, 1); + element = $compile("")($rootScope); + $rootScope.$apply(function() { + $rootScope.x = aDate; + }); + aDate.setDate(aDate.getDate()+1); + $rootScope.$apply(); + expect(element.datepicker('getDate')).toEqual(aDate); + }); + }); it('should put the date in the model', function() { inject(function($compile, $rootScope) { var aDate, element;