简体   繁体   中英

how to pass variable from one function to another function in same component.ts in angular 6

pass variable from one function to ngOnInit() function in same component.ts

form this function to :

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;
  }

this function:

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));
  }

pass this.uploadFile variable form fileChanged function to onInit() function

You can declare variable as local variable in component.ts.

let uploadFile:any;

and now use this variable in any function as this.uploadFile . Hope this will solve your problem :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM