In my angularjs application, I have the following block which is behaving weirdly.
I have declared the variable in the function block but unable to access the values in spite of setting the value inside the function block. Where I am going wrong.
$scope.submitForm = function (isValid) {
var file = $scope.myFile;
var contents;
$scope.dataObj;
if (file) {
var r = new FileReader();
r.onload = function (e) {
console.log('Inside the file function ....');
contents = e.target.result;
$scope.$apply(function () {
$scope.fileReader = contents;
contents = contents.replace(/\r\n+/g, ",");
$scope.dataObj = {hosts: contents};
console.log($scope.dataObj); // prints the value twice
});
};
r.readAsText(file);
}
console.log("Printing the data objects .... ");
console.log($scope.dataObj); // prints undefined
}