我正在尝试将文件中的文本保存到根范围内的字符串中。到目前为止,我所拥有的是一个可以正确加载的指令,以及一个检查根目录的函数
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]);
});
}
}
});
它为一个文本文件返回以下输出:
# Text file
Hello, world!
输出:
Hello, world!
function working
fileDirective.js:44
Uncaught TypeError: Cannot read property 'data' of undefined fileDirective.js:45
callData fileDirective.js:45
reader.onload