Skip to content

Instantly share code, notes, and snippets.

@martianyi
Last active March 21, 2017 02:16
Show Gist options
  • Save martianyi/420c56ec710a9daf58b7 to your computer and use it in GitHub Desktop.
Save martianyi/420c56ec710a9daf58b7 to your computer and use it in GitHub Desktop.
angularjs directive to upload file
var fileModel = angular.module('directives.fileModel', []);
fileModel.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function(){
scope.$apply(function(){
modelSetter(scope, element[0].files[0]);
});
});
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment