Skip to content

Instantly share code, notes, and snippets.

@Enome
Created October 10, 2012 15:37
Show Gist options
  • Save Enome/3866401 to your computer and use it in GitHub Desktop.
Save Enome/3866401 to your computer and use it in GitHub Desktop.
var controllers = {
UploadCtrl: function ($scope) {
$scope.images = [];
$scope.files = [];
$scope.upload = function (element) {
$scope.$apply(function ($scope) {
$scope.files = element.files;
});
};
$scope.$watch('files', function () {
for (var i = 0; i < $scope.files.length; i += 1) {
var current = $scope.files[i];
var reader = new FileReader();
reader.onload = (function (file) {
return function (env) {
console.log(file.name);
$scope.images.push({ name: file.name, src: env.target.result });
}
}(current));
reader.readAsDataURL(current);
}
});
}
};
<div ng-controller='UploadCtrl'>
<input type='file' multiple onchange='angular.element(this).scope().upload(this)' />
<div ng-repeat='image in images'>
{{image.name}}: <img ng-src='{{image.src}}' width='80' height='80' />
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment