Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(input): listen on "change" for radio and checkbox
input[radio] and inout[checkbox] now listen on the change event instead of the click event. This fixes issue with 3rd party libraries that trigger a change event on inputs, e.g. Bootstrap 3 custom checkbox / radio button toggles. It also makes it easier to prevent specific events that can cause a checkbox / radio to change, e.g. click events. Previously, this was difficult because the custom click handler had to be registered before the input directive's click handler. It is possible that radio and checkbox listened to click because IE8 has broken support for listening on change, see http://www.quirksmode.org/dom/events/change.html Closes angular#4516 Closes angular#14667 BREAKING CHANGE: input[radio] and input[checkbox] now need to be attached to the document to propagate events correctly. This should only be of concern in unit-tests that compile input elements and trigger click events on them. This is because we now listen to the change event which gets automatically triggered by browsers when a checkbox or radio is clicked. However, this may fail in some browsers when the elements are not attached to the document. Before: ```js it('should update the model', inject(function($compile, $rootScope) { var inputElm = $compile('<input type="checkbox" ng-model="checkbox" />')($rootScope); browserTrigger(inputElm[0], 'click'); expect($rootScope.checkbox).toBe(true); }); ``` With this patch, `$rootScope.checkbox` might not be true, because the click event hasn't triggered the change event. To make the test, work append the inputElm to the app's $rootElement, and the $rootElement to the $document: After: ```js it('should update the model', inject(function($compile, $rootScope, $rootElement, $document) { var inputElm = $compile('<input type="checkbox" ng-model="checkbox" />')($rootScope); $rootElement.append(inputElm); $document.append($rootElement); browserTrigger(inputElm[0], 'click'); expect($rootScope.checkbox).toBe(true); }); ```
- Loading branch information