1

Inside of an ng-repeat="item in vm.items I have the following input["file"]:

<input type="file" 
       ng-attr-id="image{{item}}"
       class="form-control"
       accept="image/*"
       onchange="angular.element(this).scope().vm.openFile(event, item)"
       required>

I am calling vm.openFile using onchange instead of ng-change because ng-change is not supported for input["file"] types.

The problem, now, is that I am unable to pass item to my onchange function because of the following error:

Uncaught ReferenceError: item is not defined

How can I pass the value of item, which is just a string, into my onchange function within ng-repeat?

Raphael Rafatpanah
  • 19,082
  • 25
  • 92
  • 158

1 Answers1

7

Since onchange is a build-in function, it is not compiled by angular but simply execute global-scope javascript. So, since there is no item variable in your global scope (which is fine), it crashes.

For the same reason as you could not simply write vm, but had to write angular.element(this).scope().vm, you will need to use the same for the item variable. Hence the html:

onchange="angular.element(this).scope().vm.openFile(event, angular.element(this).scope().item)"

Note that I don'tknow where this event variable comes from, so you may need to do the same for it if it is an angular thing.

Now this is a hack agisnt Angular design principles, so it would be better to use a directive as pointed out by David Williams comment.

floribon
  • 19,175
  • 5
  • 54
  • 66