2016-10-20 32 views
1

我使用Meteor-Angular2和彈弓包將圖像上傳到S3存儲。從函數返回並分配給綁定的字符串時,視圖未更新。 (setTimout功能正在工作和更新視圖,但載功能不)Angular2視圖在回調函數後未更新

export class AdminComponent { 

    public urlVariable: string = "ynet.co.il"; 

    constructor() { 
     this.uploader = new Slingshot.Upload("myFileUploads"); 
     setTimeout(() => { 
      this.urlVariable = "View is updated"; 
     }, 10000); 
    } 

    onFileDrop(file: File): void { 
     console.log('Got file2'); 

     this.uploader.send(file, function (error, downloadUrl) { 
      if (error) { 
       // Log service detailed response 
      } 
      else { 

       this.urlVariable = "View not updated"; 

      } 
     }); 
    } 

} 

回答

1

使用方向的功能(() =>)代替function()保留的this.

this.uploader.send(file, (error, downloadUrl) => { 
     if (error) { 
      // Log service detailed response 
     } 
     else { 
      // now `this.` points to the current class instance 
      this.urlVariable = "View not updated"; 

     } 
    }); 

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Functions/Arrow_functions

+0

仍然不更新視圖,除非我移動到路由器中的另一條路由器或執行一些觸發頁面刷新的操作。 – roish

+0

從哪裏調用'onFileDrop'? –

+1

謝謝! 與你的答案和除了this._ngZone.run(()=> {this.urlVariable =「查看未更新」;});它正在工作,並立即更新! :-) – roish

0

這一範圍正在爲我工​​作:(窄功能+ Ngzone)

this.uploader.send(file, (error, downloadUrl) => { 
     if (error) { 
      // Log service detailed response 
     } 
     else { 
      // now `this.` points to the current class instance 
      this._ngZone.run(() => {this.urlVariable = "View not updated"; }); 


     } 
    }); 
相關問題