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.