2017-03-26 50 views
0

我想發送一個文件的名稱出我可以發出輸出併發送到另一個組件的函數。我嘗試了一個原型,但我無法解決它。如何發送匿名函數變量的內容

file(file: File): void { 

    UploadFS.selectFiles(function(file){ 

     var today = new Date(); 
     var dd = today.getTime(); 
     let photo = { 
      name: file.name + dd, 
      size: file.size, 
      type: file.type 
     }; 
     let worker = new UploadFS.Uploader({ 
      store: ImagesStore, 
      data: file, 
      file: photo, 
      onComplete(file) { 
       console.log(file.name + ' has been uploaded'); 
       this.check = file.name; 

      } 
     }); 
     worker.start(); 
     this.check = photo.name; 
     console.log(photo.name); //here the name of file is visible 

    }); 

    console.log("check outside33 ",this.check); // here not: output: undefinded 
    //this.onFile.emit(this.check); 

} 

回答

1

變化

UploadFS.selectFiles(function(file){ 

UploadFS.selectFiles((file)=>{ 

this不是指的你的組件

或使用舊的JS方式:

var self = this; //<-- assign this to self here 
UploadFS.selectFiles(function(file){ 

    var today = new Date(); 
    var dd = today.getTime(); 
    let photo = { 
     name: file.name + dd, 
     size: file.size, 
     type: file.type 
    }; 
    let worker = new UploadFS.Uploader({ 
     store: ImagesStore, 
     data: file, 
     file: photo, 
     onComplete(file) { 
      console.log(file.name + ' has been uploaded'); 
      self.check = file.name; 

     } 
    }); 
    worker.start(); 
    self.check = photo.name; //<-- use self here 
    console.log(photo.name); //here the name of file is visible 

}); 
+1

謝謝!這是工作 – beciaha