I'm trying to display a list of files which the user selects using the input[type=file].
HTML
<div ng-app="myApp">
<div ng-controller="UploadFilesCtrl">
<input type="file" onchange="angular.element(this).scope().uploadFiles(this)" multiple />
<ul>
<li ng-repeat="name in filenames">
{{name}}
</li>
</ul>
<button ng-click="test()">PRINT</button>
</div>
</div>
JS
app = angular.module('myApp');
app.controller('UploadFilesCtrl', ['$scope', function($scope){
$scope.uploadFiles = function(element){
var files = element.files;
var filenames=[];
for (i=0; i<files.length; i++){
filenames.push(files[i].name);
}
$scope.files = files;
$scope.filenames = filenames;
console.log(files, filenames);
};
$scope.test = function(){
console.log($scope.files, $scope.filenames);
}
}]);
From some reason the list never gets updated.
The filenames and files variables in the $scope never get updated outside of the uploadFiles function (When I click the PRINT button after selecting file I get undefined).
Any ideas?
Thanks!
angular.element(this).scope().uploadFiles(this)I would imagine this isn't causing an angular digest. This seems like a job for adirectiveinstead, but in the mean time you can try$scope.$apply(function() { $scope.files = files; });to see if it is actually a digest issue$scope.applydoesn't do the job from some reason. I ended up trying to create a directive. Thanks!