0

i have this controller:

function controller(chatService,userService) {
        const vm = this;
        vm.postPhoto = postPhoto;

        function postPhoto(files) {
            console.log(files);
        }
    }

this is where i want to acess the function postPhoto to get the files and post to the server:

       <input type="file" id="image_uploads"
       onchange="angular.element(this).scope().postPhoto(this.files)"
       style="display:none;" accept=".jpg, .jpeg, .png">

and i got this error:

Uncaught TypeError: Cannot read property 'postPhoto' of undefined

everything works when i use "$scope" instead of "this" but i am asking how or if i can do that without changing the way i do scope..besides ng-change doesnt work on upload files

  • In case I am missing something, why are you not doing this 'the angular' way, e.g. `ng-change`? It is for this exact use case. You can change the `onchange` line to `ng-change="postPhoto(this.files)"`. – Tyler Jan 03 '18 at 19:37
  • i test here and it doesnt work on input with type=file, because of that i am trying "onchange", maybe i can do a directive, what do you think? – Maik Catrinque Jan 03 '18 at 20:02
  • I see, I did a quick search and found this [question/answer](https://stackoverflow.com/a/41557378/4606706), which uses a directive to handle this the angular way. I would recommend trying it out! – Tyler Jan 03 '18 at 21:07

1 Answers1

0

Try this:

function controller(chatService,userService) {
        var vm = this;
        function postPhoto(files) {
            console.log(files);
        }
        vm.postPhoto = postPhoto;
    }
Ritesh Ranjan
  • 1,012
  • 9
  • 16