Last active
February 9, 2016 14:42
-
-
Save phedoreanu/3b4c0c6eb3275236aabd to your computer and use it in GitHub Desktop.
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
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