Created
December 24, 2012 20:46
-
-
Save shanestillwell/4370628 to your computer and use it in GitHub Desktop.
AngularJS update model upon input blur
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Main() {} | |
// override the default input to update on blur | |
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()); | |
}); | |
}); | |
} | |
}; | |
}); | |
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="text" ng-model="value" ng-model-onblur> | |
{{value}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IE8 doesn't have the input event and will throw a mismatch error so use the $sniffer service to check prior to calling unbind.