将变量从一个函数传递到同一组件中的ngOnInit()函数。
将此功能设置为:
public fileChanged(event?: UIEvent): void {
const files: FileList = this.fileUploadEl.nativeElement.files;
const file = files[0];
const reader = new FileReader();
const loaded = (el) => {
const uploadFile = el.target.result;
this.uploadFile = uploadFile
}
reader.onload = loaded;
reader.readAsText(file, 'UTF-8');
this.name = file.name;
}
此功能:
ngOnInit() {
var dataList = Object.keys(this.data).map(key => this.data[key]);
var contents = dataList.map(d => Object.keys(d).map(key => d[key]))
var resultList = [];
if(contents && contents.length) {
var firstData = contents[0];
firstData.forEach((content,ind) => {
var resultData = {};
contents.forEach((content,index) => resultData[index] = content[ind]);
resultList.push(resultData)
})
}
this.tablelist = resultList;
// console.log(JSON.stringify(resultList));
}
将此this.uploadFile变量从fileChanged函数传递给onInit()函数
答案 0 :(得分:1)
您可以在component.ts中将变量声明为局部变量。
let uploadFile:any;
,现在在任何函数中将此变量用作this.uploadFile
。
希望这能解决您的问题:)