1

I have a file upload button, after uploading I want the selected file names to appear in the row below, using Angular ngRepeat, But the list does not appear.
Here my Code:

var pniotApp = angular.module('pniotApp', []); pniotApp.controller('pniotCtrl', function ($scope, $http, $location) { $scope.fileSelected = function (element) { $scope.files = element.files; } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body dir="rtl" ng-app="pniotApp" ng-controller="pniotCtrl"> <input id="uploadFile" type="file" class="filestyle" data-classButton="btn btn-primary" multiple data-input="false" data-classIcon="icon-plus" onchange="angular.element(this).scope().fileSelected(this)" data-buttonText="עיון..."> <ul> <li ng-repeat="file in files">{{file.name}}</li> </ul> </body>
If I try to check the value of the $scope.files I get in the console the file name

console.log($scope.files[0].name); 

What can be the reason that in the the list doesn't appear?

2
  • define $scope.files = [] above your fileselcted function. Commented Jul 31, 2017 at 7:48
  • It's work!!Thanks!!!!! Commented Jul 31, 2017 at 7:50

3 Answers 3

1

You forgot to define $scope.files.

var pniotApp = angular.module('pniotApp', []); pniotApp.controller('pniotCtrl', function ($scope, $http, $location) { $scope.files=[]; $scope.fileSelected = function (element) { $scope.files = element.files; } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body dir="rtl" ng-app="pniotApp" ng-controller="pniotCtrl"> <input id="uploadFile" type="file" class="filestyle" data-classButton="btn btn-primary" multiple data-input="false" data-classIcon="icon-plus" onchange="angular.element(this).scope().fileSelected(this)" data-buttonText="עיון..."> <ul> <li ng-repeat="file in files">{{file.name}}</li> </ul> </body>

Sign up to request clarification or add additional context in comments.

Comments

0

Define the empty array of $scope.files=[] outside the function.

Comments

0

As you have used onchange function you need to digest as it is not a angular directive

var pniotApp = angular.module('pniotApp', []); pniotApp.controller('pniotCtrl', function ($scope, $http, $location) { $scope.fileSelected = function (element) { $scope.files = element.files; $scope.$digest() } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body dir="rtl" ng-app="pniotApp" ng-controller="pniotCtrl"> <input id="uploadFile" type="file" class="filestyle" data-classButton="btn btn-primary" multiple data-input="false" data-classIcon="icon-plus" onchange="angular.element(this).scope().fileSelected(this)" data-buttonText="עיון..."> <ul> <li ng-repeat="file in files">{{file.name}}</li> </ul> </body>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.