Skip to content

Instantly share code, notes, and snippets.

@fasfsfgs
Created June 27, 2015 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fasfsfgs/8b2b8903fa92cf766a93 to your computer and use it in GitHub Desktop.
Save fasfsfgs/8b2b8903fa92cf766a93 to your computer and use it in GitHub Desktop.
Angular directive for html input file. Thanks to https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs;
(function () {
angular
.module('common')
.directive('fileModel', fileModel);
fileModel.$inject = ['$parse'];
function fileModel($parse) {
var directive = {
restrict: 'A',
link: link
};
return directive;
function link(scope, el, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
el.bind('change', onChangeHandler);
function onChangeHandler() {
scope.$apply(function applyToScope() {
var arquivoSelecionado = null;
if (el[0].files.length === 1) {
arquivoSelecionado = el[0].files[0];
}
modelSetter(scope, arquivoSelecionado);
});
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment