Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 6b1c68f

Browse files
committed
fix(datepicker): fire ngChange on today/clear button press
Closes #1379
1 parent 93cd0df commit 6b1c68f

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/datepicker/datepicker.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,10 @@ function ($compile, $parse, $document, $position, dateFilter, datepickerPopupCon
366366
ngModel.$parsers.unshift(parseDate);
367367

368368
// Inner change
369-
scope.dateSelection = function() {
369+
scope.dateSelection = function(dt) {
370+
if (angular.isDefined(dt)) {
371+
scope.date = dt;
372+
}
370373
ngModel.$setViewValue(scope.date);
371374
ngModel.$render();
372375

@@ -442,13 +445,11 @@ function ($compile, $parse, $document, $position, dateFilter, datepickerPopupCon
442445
}
443446
});
444447

445-
var $setModelValue = $parse(attrs.ngModel).assign;
446-
447448
scope.today = function() {
448-
$setModelValue(originalScope, new Date());
449+
scope.dateSelection(new Date());
449450
};
450451
scope.clear = function() {
451-
$setModelValue(originalScope, null);
452+
scope.dateSelection(null);
452453
};
453454

454455
var $popup = $compile(popupEl)(scope);

src/datepicker/test/datepicker.spec.js

+30
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,36 @@ describe('datepicker directive', function () {
11951195
expect(buttonBarElement.css('display')).toBe('none');
11961196
});
11971197
});
1198+
1199+
describe('`ng-change`', function() {
1200+
beforeEach(inject(function() {
1201+
$rootScope.changeHandler = jasmine.createSpy('changeHandler');
1202+
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup ng-change="changeHandler()"><div>')($rootScope);
1203+
$rootScope.$digest();
1204+
assignElements(wrapElement);
1205+
assignButtonBar();
1206+
}));
1207+
1208+
it('should be called when `today` is clicked', function() {
1209+
buttons.eq(0).click();
1210+
expect($rootScope.changeHandler).toHaveBeenCalled();
1211+
});
1212+
1213+
it('should not be called when `weeks` is clicked', function() {
1214+
buttons.eq(1).click();
1215+
expect($rootScope.changeHandler).not.toHaveBeenCalled();
1216+
});
1217+
1218+
it('should be called when `clear` is clicked', function() {
1219+
buttons.eq(2).click();
1220+
expect($rootScope.changeHandler).toHaveBeenCalled();
1221+
});
1222+
1223+
it('should not be called when `close` is clicked', function() {
1224+
buttons.eq(3).click();
1225+
expect($rootScope.changeHandler).not.toHaveBeenCalled();
1226+
});
1227+
});
11981228
});
11991229

12001230
describe('use with `ng-required` directive', function() {

0 commit comments

Comments
 (0)