2017-03-17 10 views
0

私は2角度を書くことES5を使用していて、私のコンポーネントの一つで、私はこのJavaScriptコードを持っている:ES5で書かれたAngular 2のコンポーネントクロージャにアクセスするには?

app.user = ng.core.Component({ 
    selector: 'user', 
    templateUrl: '/html/account/user' 
}).Class({ 
    constructor: function() { 
     this.getUserInfo(); 
    }, 
    getUserInfo: function() { 
     this.progress = true; 
     this.user = app.http.get('/accountsUser/info', {withCredentials: true}).toPromise().then(function (response) { 
      console.log(response); 
      this.progress = false; 
     }); 
    } 
}); 

それは未定義だからしかし、私のその後の関数で、私は、このプロパティにアクセスすることはできません。 をAngular 1にすると、コントローラー宣言関数を通じて$ scopeをグローバル変数として使用します。ここでは、クラス定義オブジェクトで、コールバックで使用するグローバル変数を持つことはできません。私は何をすべきか?

答えて

1

(response)=>

と変更function (response)だから、

this.user = app.http.get('/accountsUser/info', {withCredentials: true}).toPromise() 
    .then((response)=> { 
    console.log(response); 
    this.progress = false; 
}); 
のようになります。
関連する問題