I recently upgraded my AngularJS Framework from 1.2.0-rc.2 to the 1.2.0 release and came upon a strange issue that I haven't figured out a way around. The issue I had previous resolved was forcing an input field to fire on the on-blur event instead of the on-change event. The code for the directive I originally used was:
angular.module('app', []).directive('ngModelOnblur', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, elm, attr, ngModelCtrl) {
if (attr.type === 'radio' || attr.type === 'checkbox') return;
elm.unbind('input').unbind('keydown').unbind('change');
elm.bind('blur', function() {
scope.$apply(function() {
ngModelCtrl.$setViewValue(elm.val());
});
});
}
};
});
which is just using the suggestion found here https://groups.google.com/forum/?fromgroups#!searchin/angular/change$20blur/angular/LH0Q1A-qTVo/eyVIjJsFZGcJ
I've created two jsFiddles, one using AngularJS 1.2.0-rc.2 here, and the other using AngularJS 1.2.0 here.
The ngModleOnBlur
directive should remove the 'change'
binding from the <input>
element and add an explicit 'blur'
binding.
You'll notice that the fiddles behave differently, like the binding for elm.bind('blur', function(){...})
isn't actually being bound properly to the element, and it seems that elm.unbind('input').unbind('keydown').unbind('change')
doesn't work the same in 1.2.0.
I am aware of the new ng-blur
directive, but in my case I can't use that directly, but need to manually override the events bound to the element. If someone could post a working jsfiddle of how to manually override the events bound to an element, and/or explain why this has changed from 1.2.0-rc.2 to 1.2.0, that would be incredibly helpful.