0

I am a Javascript beginner. i am creating function expressions dynamically and calling parameters with JSON from text file. the functions are being created correctly with the correct parameters and looping is incrementing fine. i.e it is all working. my issue is that when i call the dynamically generated function with an onclick event nothing happens and no errors. this is the function:

function imgurl(arr) {

for(var i = 1; i < arr.length; i++) {

arr[i].fname = function() {
addImage(arr[i].image, 0.5, 0.75);
};

 /*$scope.addImage1 = function() {  //this is a sample of the function
 addImage('image.png', 0.5, 0.75);
 };*/

}
}; 

here is the onclick event

<button type="button" class="btn image1" ng-click="addImage1()">Image</button>

the code imports an image into canvas when the button is clicked. i tested the dynamically generated functions by invoking them immediately with () and as they were invoked when the page loaded the function looped and imported all the images into the canvas perfectly i.e the function expressions work as expected. but when i try the onclick event i get nothing, not even an error.

then i manually wrote out a test function expression and the onclick event works. from the code above:

$scope.addImage1 = function() {
 addImage('image.png', 0.5, 0.75);
 };

so when i manually write out the function expression then the onclick event works but when i try with the dynamically generated expressions then the onclick does not work. i just want to mention again that the dynamically generated functions do work when i invoke immediately, i just cannot call with the onclick event.

russ
  • 103
  • 1
  • 7
  • 2
    Could you please define "doesn't work"? Does it not run at all? Load the wrong image? Give an error in the console? – Chris Tavares Sep 26 '16 at 17:48
  • You should tag your post also with "angularjs" – Laurianti Sep 26 '16 at 17:51
  • 1
    Why are you starting your loop with `var i = 1` instead of `0`? – gen_Eric Sep 26 '16 at 17:51
  • Actually everything does work. the only issue is that the onclick event does nothing and no error is given when invoking. As I said above, I have tested the dynamically generated functions by invoking immediately and they do work but when I use the onclick event I get nothing, and no errors in console. – russ Sep 26 '16 at 17:57

0 Answers0