Created
June 27, 2015 20:50
-
-
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;
This file contains 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 () { | |
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