Struggling with variable binding in my angularjs/javascript code for uploading images. Can someone help me out? Here is the snippet of my code:
var image_source;
$scope.uploadedFile = function(element) {
reader.onload = function(event) {
image_source = event.target.result;
$scope.$apply(function($scope) {
$scope.files = element.files;
});
}
console.log(image_source, event.target.result, element.files[0], "***not working here***");
I've noticed that when I try to access the image_source
variable outside the function, it always returns undefined. Any idea why this might be happening?
By the way, I know how to handle this in typescript using the phatarrow operator
, but I'm not sure how to accomplish the same thing in JavaScript.