Webfreaks Webfreaks - 3 months ago 63
AngularJS Question

AngularJS file upload is not working when we use ng-change

In file upload ng-change is not working as expected in angularJS

<input type="file" ng-model="fileUpload" ng-change="setFiles(this) /"


JS:

$scope.setFiles=function(element){
console.log(element.files);
}


Here element.files is undefined

But if i chage the ng-change to onchange it's working.

<input type="file" ng-model="fileUpload" onchange="angular.element(this).scope().setFiles(this)"/>


JS:

$scope.setFiles=function(element){
console.log(element.files);
}


Here i'm getting the element.files object.

Why it is working in onchange not in ng-change?

Answer

Angular doesn't seem to support binding for <input type="file"..../>. It seems like you have to create a directive... full details here. You can also try this library

Comments