0

This is my HTML form

<form ng-submit='create()'>
    ..
    .
    .
    <input type='file' ng-model='logo' accept="image/*">
</form>

this is my controller :

$scope.create = function () {
        $scope.Ent = {}
        $scope.Ent.logo = $scope.logo;

This is my console only input file is undefined and all other fileds are alright

  • Possible duplicate of [ng-model for ](https://stackoverflow.com/questions/17063000/ng-model-for-input-type-file) – quirimmo May 25 '17 at 12:38

1 Answers1

1

ng-model won't work in input type 'file. use a custom directive to bind it

.directive("fileread", [function () {
    return {
        scope: {
            fileread: "="
        },
        link: function (scope, element, attributes) {
            element.bind("change", function (changeEvent) {
                var reader = new FileReader();
                reader.onload = function (loadEvent) {
                    scope.$apply(function () {
                        scope.fileread = loadEvent.target.result;
                    });
                }
                reader.readAsDataURL(changeEvent.target.files[0]);
            });
        }
    }
}]);

assign scope variable to fileread attribute

<form ng-submit='create()'>
    ..
    .
    .
    <input type='file' fileread='logo' accept="image/*">
</form> 
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
  • If you find an article in SO which already explains the issue, maybe instead of copying and paste exactly the same code, it would be better to mark the question as duplicate: https://stackoverflow.com/questions/17063000/ng-model-for-input-type-file p.s. or at least refer to that article in the answer – quirimmo May 25 '17 at 12:39