Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit b5c412c

Browse files
crisbetokara
authored andcommitted
fix(datepicker): updateOn not working with non-bubbling events (#9632)
Fixes the datepicker not supporting certain events inside `ngModelOptions.updateOn`. These are usually events that don't bubble (e.g. `blur`) and should be bound to the input, however since the `ng-model-options` are on the root element, Angular binds it's handlers to it. Fixes #9577.
1 parent d208ac5 commit b5c412c

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/components/datepicker/js/datepickerDirective.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,18 @@
418418

419419
// Responds to external error state changes (e.g. ng-required based on another input).
420420
ngModelCtrl.$viewChangeListeners.unshift(angular.bind(this, this.updateErrorState));
421+
422+
// Forwards any events from the input to the root element. This is necessary to get `updateOn`
423+
// working for events that don't bubble (e.g. 'blur') since Angular binds the handlers to
424+
// the `<md-datepicker>`.
425+
var updateOn = ngModelCtrl.$options && ngModelCtrl.$options.updateOn;
426+
427+
if (updateOn) {
428+
this.ngInputElement.on(
429+
updateOn,
430+
angular.bind(this.$element, this.$element.triggerHandler, updateOn)
431+
);
432+
}
421433
};
422434

423435
/**

src/components/datepicker/js/datepickerDirective.spec.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ describe('md-datepicker', function() {
353353
expect(controller.ngModelCtrl.$touched).toBe(true);
354354
});
355355

356-
it('should become touch from blurring the input', function() {
356+
it('should become touched from blurring the input', function() {
357357
populateInputElement('17/1/2015');
358358

359359
var input = angular.element(controller.inputElement);
@@ -371,6 +371,18 @@ describe('md-datepicker', function() {
371371
populateInputElement('7');
372372
expect(pageScope.myDate).toEqual(date);
373373
});
374+
375+
it('should work with ngModelOptions.updateOn', function() {
376+
var expectedDate = new Date(2015, JAN, 17);
377+
378+
createDatepickerInstance('<md-datepicker ng-model="myDate" ' +
379+
'ng-model-options="{ updateOn: \'blur\' }"></md-datepicker>');
380+
381+
populateInputElement('01/17/2015');
382+
angular.element(element.querySelector('input')).triggerHandler('blur');
383+
384+
expect(pageScope.myDate).toEqual(expectedDate);
385+
});
374386
});
375387

376388
describe('floating calendar pane', function() {

0 commit comments

Comments
 (0)