Skip to content

Instantly share code, notes, and snippets.

@bitflower
Created July 23, 2015 17:36
Show Gist options
  • Save bitflower/262b8f7309d64561c1a0 to your computer and use it in GitHub Desktop.
Save bitflower/262b8f7309d64561c1a0 to your computer and use it in GitHub Desktop.
ngModelOnChange: bind ngModel if input[type=file] to scope
(function() {
'use strict';
var app = angular.module('net.bitflower.ngModelOnChange', []);
// Listens to the change event of the input and sets Angular's value / ngModel of the element
app.directive('ngModelOnChange', function() {
return {
restrict: 'A',
require: '?ngModel',
link: function(scope, el, attrs, ngModel) {
el.bind('change', function() {
scope.$apply(function() {
ngModel.$setViewValue(el.val());
});
});
}
};
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment