Skip to content

Instantly share code, notes, and snippets.

@phedoreanu
Last active February 9, 2016 14:42
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 phedoreanu/3b4c0c6eb3275236aabd to your computer and use it in GitHub Desktop.
Save phedoreanu/3b4c0c6eb3275236aabd to your computer and use it in GitHub Desktop.
angular.module('file.upload', [])
.factory('fileSvc', ['$http', '$q', function ($http, $q) {
'use strict';
var factory = {};
factory.readFile = function (file) {
return $q(function (resolve, reject) {
if (!file)
reject('file= ' + file);
var reader = new FileReader();
reader.onload = function () {
resolve(reader.result);
reader.abort(); // TODO gc - reader = null?
};
reader.readAsText(file);
});
};
return factory;
}])
.directive('fileModel', ['$parse', function ($parse) {
'use strict';
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