I am trying to save the text from a file into a string inside the rootscope. What I have so far is a directive, which loads properly, and a function which checks the root
function callData($rootScope) {
console.log("function working");
console.log($rootScope.data);
}
angular.module('spreadsheetApp').directive('fileReader', function($rootScope) {
return {
restrict: 'E',
scope: true,
templateUrl:'views/fileReaderTemplate.html',
controller: 'fileReaderController',
link: function (scope, element, attributes, controller) {
$element.bind("change", function(event) {
var files = event.target.files;
var reader = new FileReader();
reader.onload = function() {
$rootScope.data = this.result;
console.log($rootScope.data);
callData($rootScope.data);
console.log("55");
};
reader.readAsText(files[0]);
});
}
}
});
Which returns the following ouput for a textfile that says:
# Text file
Hello, world!
Output:
Hello, world!
function working
fileDirective.js:44
Uncaught TypeError: Cannot read property 'data' of undefined fileDirective.js:45
callData fileDirective.js:45
reader.onload