diff --git a/src/datepicker/datepicker.js b/src/datepicker/datepicker.js index 691f6e05bd..d45e93464b 100644 --- a/src/datepicker/datepicker.js +++ b/src/datepicker/datepicker.js @@ -81,8 +81,10 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst if ( this.element ) { this._refreshView(); - var date = ngModelCtrl.$modelValue ? new Date(ngModelCtrl.$modelValue) : null; - ngModelCtrl.$setValidity('date-disabled', !date || (this.element && !this.isDisabled(date))); + if (ngModelCtrl.$modelValue) { + var date = ngModelCtrl.$modelValue ? new Date(ngModelCtrl.$modelValue) : null; + ngModelCtrl.$setValidity('date-disabled', !date || (this.element && !this.isDisabled(date))); + } } }; diff --git a/src/datepicker/test/datepicker.spec.js b/src/datepicker/test/datepicker.spec.js index 90f9e74d18..dad492401a 100644 --- a/src/datepicker/test/datepicker.spec.js +++ b/src/datepicker/test/datepicker.spec.js @@ -693,7 +693,40 @@ describe('datepicker directive', function () { }); }); + + describe('attribute `ng-model`', function() { + var dateFilter, today; + beforeEach(inject(function(_dateFilter_) { + dateFilter = _dateFilter_; + today = new Date(); + element = $compile('')($rootScope); + $rootScope.$digest(); + })); + + it('should work without model on day-mode', function() { + var title = dateFilter(today, 'MMMM yyyy'); + expect(getTitle()).toBe(title); + }); + + it('should work without model on month-mode', function() { + clickTitleButton(); + var title = dateFilter(today, 'yyyy'); + expect(getTitle()).toBe(title); + }); + it('should work without model on year-mode', function() { + clickTitleButton(); + clickTitleButton(); + + // Simulate datepicker's algorithm for year ranges + var start = parseInt((today.getFullYear() - 1) / 20, 10) * 20 + 1; + var end = start + 19; // The default range is 20; + var title = start + ' - ' + end; + + expect(getTitle()).toBe(title); + }); + }); + describe('attribute `starting-day`', function () { beforeEach(function() { $rootScope.startingDay = 1;