2016-10-20 12 views
1

私はS3ストレージに画像をアップロードするためにMeteor-Angular2とslingshotパッケージを使用しています。関数から戻り、バインドされた文字列に代入すると、ビューは更新されません。 (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"; }); 


     } 
    }); 
関連する問題